home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / Init.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  100KB  |  4,539 lines

  1. /*
  2. **    Init.c
  3. **
  4. **    Program initialization and shutdown routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* This will later go into the screen and window title */
  17.  
  18. #ifdef CPU_ANY
  19. STATIC STRPTR Machine = "";
  20. #else
  21. STATIC STRPTR Machine = "'020+ ";
  22. #endif
  23.  
  24.     /* The default public screen, if it is locked */
  25.  
  26. STATIC struct Screen *DefaultPubScreen;
  27.  
  28.     /* This variable helps us to remember whether the fast!
  29.      * macro panel was open or not.
  30.      */
  31.  
  32. STATIC BOOL HadFastMacros = FALSE;
  33.  
  34.     /* Remember whether we did pen allocation or not. */
  35.  
  36. STATIC BOOL AllocatedPens = FALSE;
  37.  
  38.     /* AddExtraAssignment(STRPTR LocalDir,STRPTR Assign):
  39.      *
  40.      *    Add assignments for local directories.
  41.      */
  42.  
  43. STATIC VOID
  44. AddExtraAssignment(STRPTR LocalDir,STRPTR Assign)
  45. {
  46.     UBYTE LocalBuffer[MAX_FILENAME_LENGTH];
  47.     BPTR FileLock;
  48.  
  49.         /* Add the colon, we'll need it later */
  50.  
  51.     LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,"%s:",Assign);
  52.  
  53.         /* Is the local directory present? */
  54.  
  55.     if(FileLock = Lock(LocalDir,ACCESS_READ))
  56.     {
  57.             /* Is the assignment present? */
  58.  
  59.         if(IsAssign(LocalBuffer))
  60.         {
  61.                 /* Check to see if the local directory */
  62.                 /* is already on the assignment list */
  63.  
  64.             if(LockInAssign(FileLock,LocalBuffer))
  65.             {
  66.                 UnLock(FileLock);
  67.  
  68.                 FileLock = NULL;
  69.             }
  70.         }
  71.     }
  72.  
  73.         /* Can we attach the lock to the assignment list? */
  74.  
  75.     if(FileLock)
  76.     {
  77.         Forbid();
  78.  
  79.             /* If the assignment is already present, add the */
  80.             /* new directory, else create a new assignment. */
  81.  
  82.         if(IsAssign(LocalBuffer))
  83.             AssignAdd(Assign,FileLock);
  84.         else
  85.             AssignLock(Assign,FileLock);
  86.  
  87.         Permit();
  88.     }
  89. }
  90.  
  91.     /* StatusSizeSetup():
  92.      *
  93.      *    Precalculates the size of the status line display.
  94.      */
  95.  
  96. STATIC VOID
  97. StatusSizeSetup(struct Screen *Screen,LONG *StatusWidth,LONG *StatusHeight)
  98. {
  99.     SZ_SizeSetup(Screen,(struct TextAttr *)&UserFont);
  100.  
  101.     if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0] && Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !Config->ScreenConfig->SplitStatus)
  102.     {
  103.         *StatusHeight = 0;
  104.  
  105.         return;
  106.     }
  107.  
  108.     if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED)
  109.     {
  110.         if(Config->ScreenConfig->StatusLine == STATUSLINE_COMPRESSED)
  111.         {
  112.             *StatusWidth    = 80 * UserFontWidth;
  113.             *StatusHeight    = UserFontHeight;
  114.  
  115.             if(Config->ScreenConfig->SplitStatus)
  116.             {
  117.                 *StatusWidth    += 2;
  118.                 *StatusHeight    += 2;
  119.             }
  120.         }
  121.         else
  122.         {
  123.             LONG ColumnLeft[4],ColumnWidth[4];
  124.             LONG i,Len,Max;
  125.  
  126.             *StatusWidth    = 0;
  127.             *StatusHeight    = SZ_BoxHeight(2);
  128.  
  129.             ColumnLeft[0] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_STATUS_TXT,MSG_TERMSTATUSDISPLAY_FONT_TXT,-1);
  130.             ColumnLeft[1] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_PROTOCOL_TXT,MSG_TERMSTATUSDISPLAY_TERMINAL_TXT,-1);
  131.             ColumnLeft[2] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_BAUDRATE_TXT,MSG_TERMSTATUSDISPLAY_PARAMETERS_TXT,-1);
  132.             ColumnLeft[3] = SZ_LeftOffsetN(MSG_TERMSTATUSDISPLAY_TIME_TXT,MSG_TERMSTATUSDISPLAY_ONLINE_TXT,-1);
  133.  
  134.             Max = 0;
  135.  
  136.             for(i = MSG_TERMAUX_READY_TXT ; i <= MSG_TERMAUX_HANG_UP_TXT ; i++)
  137.             {
  138.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  139.                     Max = Len;
  140.             }
  141.  
  142.             for(i = MSG_TERMSTATUSDISPLAY_FROZEN_TXT ; i <= MSG_TERMSTATUSDISPLAY_RECORDING_TXT ; i++)
  143.             {
  144.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  145.                     Max = Len;
  146.             }
  147.  
  148.             ColumnWidth[0] = Max;
  149.  
  150.             Max = SZ_BoxWidth(12);
  151.  
  152.             for(i = MSG_TERMAUX_ANSI_VT102_TXT ; i <= MSG_TERMAUX_HEX_TXT ; i++)
  153.             {
  154.                 if((Len = SZ_BoxWidth(strlen(LocaleString(i)))) > Max)
  155.                     Max = Len;
  156.             }
  157.  
  158.             ColumnWidth[1] = Max;
  159.  
  160.             Max = SZ_BoxWidth(10);
  161.  
  162.             for(i = MSG_TERMAUX_NONE_TXT ; i <= MSG_TERMAUX_SPACE_TXT ; i++)
  163.             {
  164.                 if((Len = SZ_BoxWidth(4 + strlen(LocaleString(i)))) > Max)
  165.                     Max = Len;
  166.             }
  167.  
  168.             ColumnWidth[2] = Max;
  169.  
  170.             ColumnWidth[3] = SZ_BoxWidth(8);
  171.  
  172.             for(i = 0 ; i < 4 ; i++)
  173.                 *StatusWidth += ColumnWidth[i] + ColumnLeft[i];
  174.  
  175.             *StatusWidth += 4 + 3 * InterWidth;
  176.  
  177.             if(!Config->ScreenConfig->SplitStatus)
  178.                 *StatusHeight += 4;
  179.             else
  180.                 *StatusHeight += 2;
  181.         }
  182.     }
  183.     else
  184.         *StatusHeight = 0;
  185. }
  186.  
  187.     /* LoadKeyMap(STRPTR Name):
  188.      *
  189.      *    Load a keymap file from disk.
  190.      */
  191.  
  192. STATIC struct KeyMap *
  193. LoadKeyMap(STRPTR Name)
  194. {
  195.     struct KeyMapResource *KeyMapResource;
  196.     struct KeyMap *Map = NULL;
  197.  
  198.         /* Try to get access to the list of currently loaded
  199.          * keymap files.
  200.          */
  201.  
  202.     if(KeyMapResource = (struct KeyMapResource *)OpenResource("keymap.resource"))
  203.     {
  204.         struct KeyMapNode *Node;
  205.  
  206.             /* Try to find the keymap in the list. */
  207.  
  208.         Forbid();
  209.  
  210.         if(Node = (struct KeyMapNode *)FindName(&KeyMapResource->kr_List,FilePart(Name)))
  211.             Map = &Node->kn_KeyMap;
  212.  
  213.         Permit();
  214.     }
  215.  
  216.         /* Still no keymap available? */
  217.  
  218.     if(!Map)
  219.     {
  220.         APTR OldPtr;
  221.  
  222.             /* Disable DOS requesters. */
  223.  
  224.         DisableDOSRequesters(&OldPtr);
  225.  
  226.             /* Unload the old keymap code. */
  227.  
  228.         if(KeySegment)
  229.             UnLoadSeg(KeySegment);
  230.  
  231.             /* Try to load the keymap from the
  232.              * name the user entered.
  233.              */
  234.  
  235.         if(!(KeySegment = LoadSeg(Name)))
  236.         {
  237.             UBYTE LocalBuffer[MAX_FILENAME_LENGTH];
  238.  
  239.                 /* Second try: load it from
  240.                   * the standard keymaps drawer.
  241.                   */
  242.  
  243.             strcpy(LocalBuffer,"KEYMAPS:");
  244.  
  245.             if(AddPart(LocalBuffer,FilePart(Name),sizeof(LocalBuffer)))
  246.             {
  247.                 if(!(KeySegment = LoadSeg(LocalBuffer)))
  248.                 {
  249.                     strcpy(LocalBuffer,"Devs:Keymaps");
  250.  
  251.                     if(AddPart(LocalBuffer,FilePart(Name),sizeof(LocalBuffer)))
  252.                         KeySegment = LoadSeg(LocalBuffer);
  253.                 }
  254.             }
  255.         }
  256.  
  257.             /* Did we get the keymap file? */
  258.  
  259.         if(KeySegment)
  260.         {
  261.             struct KeyMapNode *Node = (struct KeyMapNode *)&((ULONG *)BADDR(KeySegment))[1];
  262.  
  263.             Map = &Node->kn_KeyMap;
  264.         }
  265.  
  266.             /* Enable DOS requesters again. */
  267.  
  268.         EnableDOSRequesters(OldPtr);
  269.     }
  270.     else
  271.     {
  272.         if(KeySegment)
  273.         {
  274.             UnLoadSeg(KeySegment);
  275.  
  276.             KeySegment = NULL;
  277.         }
  278.     }
  279.  
  280.     return(Map);
  281. }
  282.  
  283.     /* DeleteOffsetTables(VOID):
  284.      *
  285.      *    Delete the line multiplication tables.
  286.      */
  287.  
  288. STATIC VOID
  289. DeleteOffsetTables(VOID)
  290. {
  291.     FreeVecPooled(OffsetXTable);
  292.     OffsetXTable = NULL;
  293.  
  294.     FreeVecPooled(OffsetYTable);
  295.     OffsetYTable = NULL;
  296. }
  297.  
  298.     /* CreateOffsetTables(VOID):
  299.      *
  300.      *    Allocate the line multiplication tables.
  301.      */
  302.  
  303. STATIC BOOL
  304. CreateOffsetTables(VOID)
  305. {
  306.     LONG Width,Height;
  307.  
  308.     Width    = (Window->WScreen->Width  + TextFontWidth)  * 2 / TextFontWidth;
  309.     Height    = (Window->WScreen->Height + TextFontHeight) * 2 / TextFontHeight;
  310.  
  311.     DeleteOffsetTables();
  312.  
  313.     if(OffsetXTable = (LONG *)AllocVecPooled(Width * sizeof(LONG),MEMF_ANY))
  314.     {
  315.         if(OffsetYTable = (LONG *)AllocVecPooled(Height * sizeof(LONG),MEMF_ANY))
  316.         {
  317.             LONG i,j;
  318.  
  319.             for(i = j = 0 ; i < Width ; i++, j += TextFontWidth)
  320.                 OffsetXTable[i] = j;
  321.  
  322.             for(i = j = 0 ; i < Height ; i++, j += TextFontHeight)
  323.                 OffsetYTable[i] = j;
  324.  
  325.             return(TRUE);
  326.         }
  327.     }
  328.  
  329.     DeleteOffsetTables();
  330.  
  331.     return(FALSE);
  332. }
  333.  
  334.     /* ReleaseDefaultPubScreen():
  335.      *
  336.      *    Unlocks the default public screen in case it had been locked.
  337.      */
  338.  
  339. STATIC VOID
  340. ReleaseDefaultPubScreen(VOID)
  341. {
  342.     if(DefaultPubScreen)
  343.     {
  344.         UnlockPubScreen(NULL,DefaultPubScreen);
  345.         DefaultPubScreen = NULL;
  346.     }
  347. }
  348.  
  349.     /* SafeOpenLibrary(STRPTR Name,LONG Version):
  350.      *
  351.      *    Try to open a library, but if there already is
  352.      *    a version in memory that's older than the release
  353.      *    we want flush it out first.
  354.      */
  355.  
  356. struct Library *
  357. SafeOpenLibrary(STRPTR Name,LONG Version)
  358. {
  359.     struct Library *Base;
  360.  
  361.     Forbid();
  362.  
  363.         /* Is this library already in memory? */
  364.  
  365.     if(Base = (struct Library *)FindName(&SysBase->LibList,FilePart(Name)))
  366.     {
  367.             /* An old release? */
  368.  
  369.         if(Base->lib_Version < Version)
  370.         {
  371.                 /* Flush it out. */
  372.  
  373.             RemLibrary(Base);
  374.         }
  375.     }
  376.  
  377.     Permit();
  378.  
  379.         /* Now reopen the library. */
  380.  
  381.     return(OpenLibrary(Name,Version));
  382. }
  383.  
  384.     /* TTYResize():
  385.      *
  386.      *    Signal AmigaUW that the window size has changed.
  387.      */
  388.  
  389. VOID
  390. TTYResize()
  391. {
  392.     if(Window && WriteRequest)
  393.     {
  394.         LONG Columns,Lines;
  395.  
  396.         if(XEmulatorBase && XEM_IO)
  397.         {
  398.             if(XEmulatorBase->lib_Version < 4)
  399.                 Columns = Lines = 0;
  400.             else
  401.             {
  402.                 ULONG Result = XEmulatorInfo(XEM_IO,XEMI_CONSOLE_DIMENSIONS);
  403.  
  404.                 Columns    = XEMI_EXTRACT_COLUMNS(Result);
  405.                 Lines    = XEMI_EXTRACT_LINES(Result);
  406.             }
  407.         }
  408.         else
  409.         {
  410.             Columns    = LastColumn + 1;
  411.             Lines    = LastLine + 1;
  412.         }
  413.  
  414.         if(Columns > 0 && Lines > 0)
  415.         {
  416.             WriteRequest->IOSer.io_Command    = UWCMD_TTYRESIZE;
  417.             WriteRequest->IOSer.io_Data        = (APTR)((Columns << 16) | (Lines));
  418.             WriteRequest->IOSer.io_Length    = (WindowWidth << 16) | (WindowHeight);
  419.  
  420.             DoIO((struct IORequest *)WriteRequest);
  421.         }
  422.     }
  423. }
  424.  
  425.     /* UpdateTerminalLimits():
  426.      *
  427.      *    Check the current window size and extract the
  428.      *    size and position of the usable window rectangle.
  429.      */
  430.  
  431. VOID
  432. UpdateTerminalLimits()
  433. {
  434.     WindowLeft        = Window->BorderLeft;
  435.     WindowTop        = Window->BorderTop;
  436.  
  437.     WindowWidth        = Window->Width - (Window->BorderLeft + Window->BorderRight);
  438.     WindowHeight    = Window->Height - (Window->BorderTop + Window->BorderBottom);
  439.  
  440.     if(StatusWindow)
  441.     {
  442.         StatusDisplayLeft    = StatusWindow->BorderLeft;
  443.         StatusDisplayTop    = StatusWindow->BorderTop;
  444.         StatusDisplayWidth    = StatusWindow->Width - (StatusWindow->BorderLeft + StatusWindow->BorderRight);
  445.         StatusDisplayHeight    = StatusWindow->Height - (StatusWindow->BorderTop + StatusWindow->BorderBottom);
  446.     }
  447.     else
  448.     {
  449.         if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  450.         {
  451.             StatusDisplayLeft    = WindowLeft;
  452.             StatusDisplayTop    = Window->Height - (Window->BorderBottom + StatusDisplayHeight);
  453.             StatusDisplayWidth    = WindowWidth;
  454.  
  455.             WindowHeight -= StatusDisplayHeight;
  456.         }
  457.     }
  458.  
  459.     if(ChatMode && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  460.     {
  461.         if(CreateChatGadget())
  462.         {
  463.             UpdateChatGadget();
  464.  
  465.             WindowHeight -= (UserFontHeight + 2);
  466.  
  467.             ActivateChat(FALSE);
  468.         }
  469.     }
  470. }
  471.  
  472.     /* Current2DefaultPalette(struct Configuration *SomeConfig):
  473.      *
  474.      *    Copy the current colour palette into the
  475.      *    default tables.
  476.      */
  477.  
  478. VOID
  479. Current2DefaultPalette(struct Configuration *SomeConfig)
  480. {
  481.     ColourTable    *Table;
  482.     UWORD *Colour12;
  483.  
  484.     Table = NULL;
  485.  
  486.     if(!SomeConfig)
  487.         SomeConfig = Config;
  488.  
  489.     switch(SomeConfig->ScreenConfig->ColourMode)
  490.     {
  491.         case COLOUR_EIGHT:
  492.  
  493.             if(Kick30)
  494.             {
  495.                 if(!ANSIColourTable)
  496.                     ANSIColourTable = CreateColourTable(8,ANSIColours,NULL);
  497.  
  498.                 Table = ANSIColourTable;
  499.             }
  500.  
  501.             Colour12 = ANSIColours;
  502.  
  503.             break;
  504.  
  505.         case COLOUR_SIXTEEN:
  506.  
  507.             if(Kick30)
  508.             {
  509.                 if(!EGAColourTable)
  510.                     EGAColourTable = CreateColourTable(16,EGAColours,NULL);
  511.  
  512.                 Table = EGAColourTable;
  513.             }
  514.  
  515.             Colour12 = EGAColours;
  516.  
  517.             break;
  518.  
  519.         case COLOUR_AMIGA:
  520.  
  521.             if(Kick30)
  522.             {
  523.                 if(!DefaultColourTable)
  524.                     DefaultColourTable = CreateColourTable(16,DefaultColours,NULL);
  525.  
  526.                 Table = DefaultColourTable;
  527.             }
  528.  
  529.             Colour12 = DefaultColours;
  530.  
  531.             break;
  532.  
  533.         case COLOUR_MONO:
  534.  
  535.             if(Kick30)
  536.             {
  537.                 if(!MonoColourTable)
  538.                     MonoColourTable = CreateColourTable(2,AtomicColours,NULL);
  539.  
  540.                 Table = MonoColourTable;
  541.             }
  542.  
  543.             Colour12 = AtomicColours;
  544.             break;
  545.     }
  546.  
  547.     if(Table)
  548.     {
  549.         if(SomeConfig->ScreenConfig->UseColours96)
  550.             Colour96xColourTable(SomeConfig->ScreenConfig->Colours96,Table,Table->NumColours);
  551.         else
  552.         {
  553.             Colour12xColourTable(SomeConfig->ScreenConfig->Colours,Table,Table->NumColours);
  554.  
  555.             Colour12x96(SomeConfig->ScreenConfig->Colours,SomeConfig->ScreenConfig->Colours96,16);
  556.  
  557.             SomeConfig->ScreenConfig->UseColours96 = TRUE;
  558.         }
  559.     }
  560.  
  561.     CopyMem(SomeConfig->ScreenConfig->Colours,Colour12,16 * sizeof(UWORD));
  562. }
  563.  
  564.     /* Default2CurrentPalette(struct Configuration *SomeConfig):
  565.      *
  566.      *    Copy the default palette to the current palette.
  567.      */
  568.  
  569. VOID
  570. Default2CurrentPalette(struct Configuration *SomeConfig)
  571. {
  572.     ColourTable    *Table;
  573.     UWORD *Colour12;
  574.  
  575.     Table = NULL;
  576.  
  577.     if(!SomeConfig)
  578.         SomeConfig = Config;
  579.  
  580.     switch(SomeConfig->ScreenConfig->ColourMode)
  581.     {
  582.         case COLOUR_EIGHT:
  583.  
  584.             Table        = ANSIColourTable;
  585.             Colour12    = ANSIColours;
  586.  
  587.             break;
  588.  
  589.         case COLOUR_SIXTEEN:
  590.  
  591.             Table        = EGAColourTable;
  592.             Colour12    = EGAColours;
  593.  
  594.             break;
  595.  
  596.         case COLOUR_AMIGA:
  597.  
  598.             Table        = DefaultColourTable;
  599.             Colour12    = DefaultColours;
  600.  
  601.             break;
  602.  
  603.         case COLOUR_MONO:
  604.  
  605.             Table        = MonoColourTable;
  606.             Colour12    = AtomicColours;
  607.  
  608.             break;
  609.     }
  610.  
  611.     CopyMem(Colour12,SomeConfig->ScreenConfig->Colours,16 * sizeof(UWORD));
  612.  
  613.     if(Table)
  614.     {
  615.         ColourTablex96(Table,SomeConfig->ScreenConfig->Colours96);
  616.  
  617.         SomeConfig->ScreenConfig->UseColours96 = TRUE;
  618.     }
  619.     else
  620.         SomeConfig->ScreenConfig->UseColours96 = FALSE;
  621. }
  622.  
  623.     /* PaletteSetup():
  624.      *
  625.      *    Set up colour palettes.
  626.      */
  627.  
  628. VOID
  629. PaletteSetup(struct Configuration *SomeConfig)
  630. {
  631.     LONG i;
  632.  
  633.     if(!SomeConfig)
  634.         SomeConfig = Config;
  635.  
  636.     if(SomeConfig->ScreenConfig->UseColours96)
  637.     {
  638.         Colour96x12(SomeConfig->ScreenConfig->Colours96,NormalColours,16);
  639.         Colour96x12(SomeConfig->ScreenConfig->Colours96,SomeConfig->ScreenConfig->Colours,16);
  640.     }
  641.     else
  642.     {
  643.         CopyMem(SomeConfig->ScreenConfig->Colours,NormalColours,16 * sizeof(UWORD));
  644.  
  645.         Colour12x96(NormalColours,SomeConfig->ScreenConfig->Colours96,16);
  646.  
  647.         SomeConfig->ScreenConfig->UseColours96 = TRUE;
  648.     }
  649.  
  650.     CopyMem(NormalColours,&NormalColours[16],16 * sizeof(UWORD));
  651.     CopyMem(NormalColours,BlinkColours,32 * sizeof(UWORD));
  652.  
  653.     switch(SomeConfig->ScreenConfig->ColourMode)
  654.     {
  655.         case COLOUR_EIGHT:
  656.  
  657.             if(SomeConfig->ScreenConfig->Blinking)
  658.             {
  659.                 for(i = 0 ; i < 8 ; i++)
  660.                     BlinkColours[8 + i] = BlinkColours[0];
  661.  
  662.                 PaletteSize = 16;
  663.             }
  664.             else
  665.                 PaletteSize = 8;
  666.  
  667.             break;
  668.  
  669.         case COLOUR_SIXTEEN:
  670.  
  671.             if(GetBitMapDepth(Window->WScreen->RastPort.BitMap) >= 5 && SomeConfig->ScreenConfig->Blinking)
  672.             {
  673.                 for(i = 0 ; i < 16 ; i++)
  674.                     BlinkColours[16 + i] = BlinkColours[0];
  675.  
  676.                 PaletteSize = 32;
  677.             }
  678.             else
  679.                 PaletteSize = 16;
  680.  
  681.             break;
  682.  
  683.         case COLOUR_AMIGA:
  684.  
  685.             BlinkColours[3] = BlinkColours[0];
  686.  
  687.             PaletteSize = 4;
  688.  
  689.             break;
  690.  
  691.         case COLOUR_MONO:
  692.  
  693.             PaletteSize = 2;
  694.             break;
  695.     }
  696.  
  697.     if(Kick30)
  698.     {
  699.         if(NormalColourTable)
  700.             DeleteColourTable(NormalColourTable);
  701.  
  702.         if(BlinkColourTable)
  703.             DeleteColourTable(BlinkColourTable);
  704.  
  705.         if(NormalColourTable = CreateColourTable(PaletteSize,NULL,SomeConfig->ScreenConfig->Colours96))
  706.         {
  707.             if(PaletteSize == 2 || !SomeConfig->ScreenConfig->Blinking)
  708.                 BlinkColourTable = NULL;
  709.             else
  710.             {
  711.                 if(BlinkColourTable = CreateColourTable(PaletteSize,NULL,NULL))
  712.                 {
  713.                     switch(SomeConfig->ScreenConfig->ColourMode)
  714.                     {
  715.                         case COLOUR_EIGHT:
  716.  
  717.                             for(i = 0 ; i < 8 ; i++)
  718.                             {
  719.                                 CopyColourEntry(NormalColourTable,NormalColourTable,i,8 + i);
  720.                                 CopyColourEntry(NormalColourTable,BlinkColourTable,i,i);
  721.                                 CopyColourEntry(NormalColourTable,BlinkColourTable,0,8 + i);
  722.                             }
  723.  
  724.                             break;
  725.  
  726.                         case COLOUR_SIXTEEN:
  727.  
  728.                             if(GetBitMapDepth(Window->WScreen->RastPort.BitMap) >= 5)
  729.                             {
  730.                                 for(i = 0 ; i < 16 ; i++)
  731.                                 {
  732.                                     CopyColourEntry(NormalColourTable,NormalColourTable,i,16 + i);
  733.                                     CopyColourEntry(NormalColourTable,BlinkColourTable,i,i);
  734.                                     CopyColourEntry(NormalColourTable,BlinkColourTable,0,16 + i);
  735.                                 }
  736.                             }
  737.                             else
  738.                             {
  739.                                 DeleteColourTable(BlinkColourTable);
  740.  
  741.                                 BlinkColourTable = NULL;
  742.                             }
  743.  
  744.                             break;
  745.  
  746.                         case COLOUR_AMIGA:
  747.  
  748.                             for(i = 0 ; i < 4 ; i++)
  749.                                 CopyColourEntry(NormalColourTable,BlinkColourTable,i,i);
  750.  
  751.                             CopyColourEntry(BlinkColourTable,BlinkColourTable,0,3);
  752.  
  753.                             break;
  754.                     }
  755.                 }
  756.                 else
  757.                 {
  758.                     DeleteColourTable(NormalColourTable);
  759.  
  760.                     NormalColourTable = NULL;
  761.                 }
  762.             }
  763.         }
  764.     }
  765. }
  766.  
  767.     /* ScreenSizeStuff():
  768.      *
  769.      *    Set up the terminal screen size.
  770.      */
  771.  
  772. VOID
  773. ScreenSizeStuff()
  774. {
  775.     ObtainSemaphore(&TerminalSemaphore);
  776.  
  777.         /* Is this really the built-in emulation? */
  778.  
  779.     if(Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL)
  780.     {
  781.         LONG MaxColumns,MaxLines,Columns,Lines;
  782.  
  783.         MaxColumns    = WindowWidth / TextFontWidth;
  784.         MaxLines    = WindowHeight / TextFontHeight;
  785.  
  786.             /* Drop the text area marker. */
  787.  
  788.         if(Marking)
  789.             WindowMarkerStop();
  790.  
  791.             /* Turn off the cursor. */
  792.  
  793.         ClearCursor();
  794.  
  795.             /* Set up the new screen width. */
  796.  
  797.         if(Config->TerminalConfig->NumColumns < 20)
  798.             Columns = MaxColumns;
  799.         else
  800.             Columns = Config->TerminalConfig->NumColumns;
  801.  
  802.             /* Set up the new screen height. */
  803.  
  804.         if(Config->TerminalConfig->NumLines < 20)
  805.             Lines = MaxLines;
  806.         else
  807.             Lines = Config->TerminalConfig->NumLines;
  808.  
  809.             /* More columns than we will be able to display? */
  810.  
  811.         if(Columns > MaxColumns)
  812.             Columns = MaxColumns;
  813.  
  814.             /* More lines than we will be able to display? */
  815.  
  816.         if(Lines > MaxLines)
  817.             Lines = MaxLines;
  818.  
  819.             /* Set up the central data. */
  820.  
  821.         LastColumn    = Columns - 1;
  822.         LastLine    = Lines - 1;
  823.         LastPixel    = MUL_X(Columns) - 1;
  824.  
  825.             /* Are we to clear the margin? */
  826.  
  827.         if(Columns < MaxColumns || Lines < MaxLines)
  828.         {
  829.                 /* Save the rendering attributes. */
  830.  
  831.             BackupRender();
  832.  
  833.                 /* Set the defaults. */
  834.  
  835.             SetAPen(RPort,BgPen = MappedPens[0][PenTable[0]]);
  836.  
  837.             SetMask(RPort,DepthMask);
  838.  
  839.                 /* Clear remaining columns. */
  840.  
  841.             if(Columns < MaxColumns)
  842.                 ScrollLineRectFill(RPort,MUL_X(LastColumn + 1),0,WindowWidth - 1,WindowHeight - 1);
  843.  
  844.                 /* Clear remaining lines. */
  845.  
  846.             if(Lines < MaxLines)
  847.                 ScrollLineRectFill(RPort,0,MUL_Y(LastLine + 1),WindowWidth - 1,WindowHeight - 1);
  848.  
  849.                 /* Restore rendering attributes. */
  850.  
  851.             BackupRender();
  852.         }
  853.  
  854.             /* Truncate illegal cursor position. */
  855.  
  856.         if(CursorY > LastLine)
  857.             CursorY = LastLine;
  858.  
  859.         ConFontScaleUpdate();
  860.  
  861.             /* Truncate illegal cursor position. */
  862.  
  863.         if(CursorX > LastPrintableColumn)
  864.             CursorX = LastPrintableColumn;
  865.  
  866.             /* Fix scroll region button. */
  867.  
  868.         if(!RegionSet)
  869.             Bottom = LastLine;
  870.  
  871.             /* Turn the cursor back on. */
  872.  
  873.         DrawCursor();
  874.     }
  875.  
  876.     FixScreenSize = FALSE;
  877.  
  878.     ReleaseSemaphore(&TerminalSemaphore);
  879. }
  880.  
  881.     /* PubScreenStuff():
  882.      *
  883.      *    This part handles the public screen setup stuff.
  884.      */
  885.  
  886. VOID
  887. PubScreenStuff()
  888. {
  889.     if(Screen)
  890.     {
  891.             /* Are we to make our screen public? */
  892.  
  893.         if(Config->ScreenConfig->MakeScreenPublic)
  894.             PubScreenStatus(Screen,NULL);
  895.         else
  896.             PubScreenStatus(Screen,PSNF_PRIVATE);
  897.     }
  898. }
  899.  
  900.     /* ConfigSetup():
  901.      *
  902.      *    Compare the current configuration with the
  903.      *    last backup and reset the serial device, terminal,
  904.      *    etc. if necessary.
  905.      */
  906.  
  907. VOID
  908. ConfigSetup()
  909. {
  910.     BOOL RasterWasEnabled,WindowSizeUpdate,WantCustomScreen;
  911.  
  912.     RasterWasEnabled = RasterEnabled;
  913.     WindowSizeUpdate = FALSE;
  914.  
  915.         /* See if we want a custom screen */
  916.  
  917.     if(Config->ScreenConfig->UseWorkbench || Config->ScreenConfig->ShareScreen)
  918.         WantCustomScreen = FALSE;
  919.     else
  920.         WantCustomScreen = TRUE;
  921.  
  922.         /* Hide or show the upload queue icon. */
  923.  
  924.     ToggleUploadQueueIcon(Config->TransferConfig->HideUploadIcon);
  925.  
  926.         /* Take care of the end-of-line translation. */
  927.  
  928.     Update_CR_LF_Translation();
  929.  
  930.         /* First we will take a look at the configuration
  931.          * and try to find those parts which have changed
  932.          * and require the main screen display to be
  933.          * reopened.
  934.          */
  935.  
  936.     if(PrivateConfig->ScreenConfig->FontHeight != Config->ScreenConfig->FontHeight)
  937.         ResetDisplay = TRUE;
  938.  
  939.     if((PrivateConfig->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL) || (PrivateConfig->TerminalConfig->EmulationMode != EMULATION_EXTERNAL && Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL))
  940.         ResetDisplay = TRUE;
  941.  
  942.     if(PrivateConfig->ScreenConfig->SplitStatus != Config->ScreenConfig->SplitStatus)
  943.         ResetDisplay = TRUE;
  944.  
  945.     if(PrivateConfig->ScreenConfig->ShareScreen != Config->ScreenConfig->ShareScreen)
  946.         ResetDisplay = TRUE;
  947.  
  948.     if(PrivateConfig->ScreenConfig->Depth != Config->ScreenConfig->Depth || PrivateConfig->ScreenConfig->OverscanType != Config->ScreenConfig->OverscanType)
  949.         ResetDisplay = TRUE;
  950.  
  951.     if(PrivateConfig->ScreenConfig->DisplayWidth != Config->ScreenConfig->DisplayWidth || PrivateConfig->ScreenConfig->DisplayHeight != Config->ScreenConfig->DisplayHeight)
  952.         ResetDisplay = TRUE;
  953.  
  954.     if(Stricmp(PrivateConfig->ScreenConfig->FontName,Config->ScreenConfig->FontName))
  955.         ResetDisplay = TRUE;
  956.  
  957.     if(PrivateConfig->TerminalConfig->FontMode != Config->TerminalConfig->FontMode)
  958.         ResetDisplay = TRUE;
  959.  
  960.     if(PrivateConfig->TerminalConfig->UseTerminalTask != Config->TerminalConfig->UseTerminalTask)
  961.         ResetDisplay = TRUE;
  962.  
  963.     if(PrivateConfig->TerminalConfig->AutoSize != Config->TerminalConfig->AutoSize && !WantCustomScreen)
  964.     {
  965.         if(Config->TerminalConfig->AutoSize)
  966.             FixScreenSize = TRUE;
  967.         else
  968.             WindowSizeUpdate = TRUE;
  969.     }
  970.  
  971.     if(PrivateConfig->TerminalConfig->TextFontHeight != Config->TerminalConfig->TextFontHeight)
  972.         ResetDisplay = TRUE;
  973.  
  974.     if(Stricmp(PrivateConfig->TerminalConfig->TextFontName,Config->TerminalConfig->TextFontName))
  975.         ResetDisplay = TRUE;
  976.  
  977.     if(PrivateConfig->ScreenConfig->DisplayMode != Config->ScreenConfig->DisplayMode || PrivateConfig->ScreenConfig->ColourMode != Config->ScreenConfig->ColourMode)
  978.         ResetDisplay = TRUE;
  979.  
  980.     if(PrivateConfig->TerminalConfig->IBMFontHeight != Config->TerminalConfig->IBMFontHeight)
  981.         ResetDisplay = TRUE;
  982.  
  983.     if(Stricmp(PrivateConfig->TerminalConfig->IBMFontName,Config->TerminalConfig->IBMFontName))
  984.         ResetDisplay = TRUE;
  985.  
  986.     if((PrivateConfig->ScreenConfig->ColourMode == Config->ScreenConfig->ColourMode) && (PrivateConfig->ScreenConfig->ColourMode == COLOUR_EIGHT || PrivateConfig->ScreenConfig->ColourMode == COLOUR_SIXTEEN))
  987.     {
  988.         if(PrivateConfig->ScreenConfig->Blinking != Config->ScreenConfig->Blinking)
  989.             ResetDisplay = TRUE;
  990.     }
  991.  
  992.     if(Kick30)
  993.     {
  994.         if(Config->ScreenConfig->UsePens != PrivateConfig->ScreenConfig->UsePens || memcmp(Config->ScreenConfig->PenArray,PrivateConfig->ScreenConfig->PenArray,sizeof(UWORD) * 12))
  995.             ResetDisplay = TRUE;
  996.     }
  997.  
  998.     if(Config->ScreenConfig->Depth != PrivateConfig->ScreenConfig->Depth)
  999.         ResetDisplay = TRUE;
  1000.  
  1001.     if(PrivateConfig->ScreenConfig->FasterLayout != Config->ScreenConfig->FasterLayout)
  1002.         ResetDisplay = TRUE;
  1003.  
  1004.     if(PrivateConfig->ScreenConfig->StatusLine != Config->ScreenConfig->StatusLine)
  1005.         ResetDisplay = TRUE;
  1006.  
  1007.     if(PrivateConfig->ScreenConfig->TitleBar != Config->ScreenConfig->TitleBar)
  1008.         ResetDisplay = TRUE;
  1009.  
  1010.     if(PrivateConfig->ScreenConfig->UseWorkbench != Config->ScreenConfig->UseWorkbench)
  1011.         ResetDisplay = TRUE;
  1012.  
  1013.     if(strcmp(PrivateConfig->ScreenConfig->PubScreenName,Config->ScreenConfig->PubScreenName) && Config->ScreenConfig->UseWorkbench)
  1014.         ResetDisplay = TRUE;
  1015.  
  1016.     if(PrivateConfig->EmulationConfig->UseStandardPens != Config->EmulationConfig->UseStandardPens)
  1017.         ResetDisplay = TRUE;
  1018.  
  1019.     if(!Config->EmulationConfig->UseStandardPens && (memcmp(Config->EmulationConfig->Pens,PrivateConfig->EmulationConfig->Pens,sizeof(Config->EmulationConfig->Pens)) || memcmp(Config->EmulationConfig->Attributes,PrivateConfig->EmulationConfig->Attributes,sizeof(Config->EmulationConfig->Attributes))))
  1020.         ResetDisplay = TRUE;
  1021.  
  1022.         /* Now for the `harmless' actions which do not
  1023.          * require to change the screen or other
  1024.          * rendering data.
  1025.          */
  1026.  
  1027.     if(!ResetDisplay)
  1028.     {
  1029.         if(PrivateConfig->TerminalConfig->NumColumns != Config->TerminalConfig->NumColumns || PrivateConfig->TerminalConfig->NumLines != Config->TerminalConfig->NumLines)
  1030.         {
  1031.             if(!WantCustomScreen)
  1032.                 WindowSizeUpdate = TRUE;
  1033.             else
  1034.                 ResetDisplay = TRUE;
  1035.         }
  1036.     }
  1037.  
  1038.     if(!ResetDisplay && WindowSizeUpdate)
  1039.     {
  1040.         LONG    Width,
  1041.             Height;
  1042.  
  1043.         if(Config->TerminalConfig->NumColumns < 20)
  1044.             Width = ScreenWidth;
  1045.         else
  1046.             Width = Window->BorderLeft + TextFontWidth * Config->TerminalConfig->NumColumns + Window->BorderRight;
  1047.  
  1048.         if(Config->TerminalConfig->NumLines < 20)
  1049.             Height = ScreenHeight;
  1050.         else
  1051.             Height = Window->BorderTop + TextFontHeight * Config->TerminalConfig->NumLines + Window->BorderBottom;
  1052.  
  1053.             /* Add the status line size if necessary */
  1054.  
  1055.         if(!StatusWindow && Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  1056.             Height += StatusDisplayHeight;
  1057.  
  1058.         ChangeWindowBox(Window,Window->LeftEdge,Window->TopEdge,Width,Height);
  1059.  
  1060.         FixScreenSize = TRUE;
  1061.     }
  1062.  
  1063.     if(PrivateConfig->ScreenConfig->MakeScreenPublic != Config->ScreenConfig->MakeScreenPublic)
  1064.         PubScreenStuff();
  1065.  
  1066.     if(PrivateConfig->ScreenConfig->ColourMode == Config->ScreenConfig->ColourMode && (memcmp(PrivateConfig->ScreenConfig->Colours,Config->ScreenConfig->Colours,sizeof(UWORD) * 16) || memcmp(PrivateConfig->ScreenConfig->Colours96,Config->ScreenConfig->Colours96,sizeof(ColourEntry) * 16)))
  1067.         Current2DefaultPalette(Config);
  1068.  
  1069.         /* Are we to load a new transfer library? */
  1070.  
  1071.     if(Config->TransferConfig->DefaultType == XFER_XPR)
  1072.     {
  1073.         if(Config->TransferConfig->DefaultLibrary[0])
  1074.         {
  1075.             if(strcmp(PrivateConfig->TransferConfig->DefaultLibrary,Config->TransferConfig->DefaultLibrary))
  1076.             {
  1077.                 strcpy(LastXprLibrary,Config->TransferConfig->DefaultLibrary);
  1078.  
  1079.                 ProtocolSetup(FALSE);
  1080.             }
  1081.         }
  1082.         else
  1083.             CloseXPR();
  1084.     }
  1085.     else
  1086.     {
  1087.         CloseXPR();
  1088.  
  1089.         ProtocolSetup(FALSE);
  1090.     }
  1091.  
  1092.         /* No custom keymap this time? */
  1093.  
  1094.     if(!Config->TerminalConfig->KeyMapFileName[0])
  1095.     {
  1096.         KeyMap = NULL;
  1097.  
  1098.         if(KeySegment)
  1099.         {
  1100.             UnLoadSeg(KeySegment);
  1101.  
  1102.             KeySegment = NULL;
  1103.         }
  1104.     }
  1105.     else
  1106.     {
  1107.             /* Check whether the keymap name has changed. */
  1108.  
  1109.         if(strcmp(PrivateConfig->TerminalConfig->KeyMapFileName,Config->TerminalConfig->KeyMapFileName))
  1110.             KeyMap = LoadKeyMap(Config->TerminalConfig->KeyMapFileName);
  1111.     }
  1112.  
  1113.         /* Are we to load the keyboard macro settings? */
  1114.  
  1115.     if(Config->MacroFileName[0] && Stricmp(PrivateConfig->MacroFileName,Config->MacroFileName))
  1116.     {
  1117.         if(!LoadMacros(Config->MacroFileName,MacroKeys))
  1118.             ResetMacroKeys(MacroKeys);
  1119.         else
  1120.             strcpy(LastMacros,Config->MacroFileName);
  1121.     }
  1122.  
  1123.         /* Are we to load the cursor key settings? */
  1124.  
  1125.     if(Config->CursorFileName[0] && Stricmp(PrivateConfig->CursorFileName,Config->CursorFileName))
  1126.     {
  1127.         if(!ReadIFFData(Config->CursorFileName,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  1128.             ResetCursorKeys(CursorKeys);
  1129.         else
  1130.             strcpy(LastCursorKeys,Config->CursorFileName);
  1131.     }
  1132.  
  1133.         /* Are we to load the translation tables? */
  1134.  
  1135.     if(Config->TranslationFileName[0] && Stricmp(PrivateConfig->TranslationFileName,Config->TranslationFileName))
  1136.     {
  1137.         if(SendTable)
  1138.         {
  1139.             FreeTranslationTable(SendTable);
  1140.  
  1141.             SendTable = NULL;
  1142.         }
  1143.  
  1144.         if(ReceiveTable)
  1145.         {
  1146.             FreeTranslationTable(ReceiveTable);
  1147.  
  1148.             ReceiveTable = NULL;
  1149.         }
  1150.  
  1151.         if(SendTable = AllocTranslationTable())
  1152.         {
  1153.             if(ReceiveTable = AllocTranslationTable())
  1154.             {
  1155.                 if(LoadTranslationTables(Config->TranslationFileName,SendTable,ReceiveTable))
  1156.                 {
  1157.                     strcpy(LastTranslation,Config->TranslationFileName);
  1158.  
  1159.                     if(IsStandardTable(SendTable) && IsStandardTable(ReceiveTable))
  1160.                     {
  1161.                         FreeTranslationTable(SendTable);
  1162.  
  1163.                         SendTable = NULL;
  1164.  
  1165.                         FreeTranslationTable(ReceiveTable);
  1166.  
  1167.                         ReceiveTable = NULL;
  1168.                     }
  1169.                 }
  1170.                 else
  1171.                 {
  1172.                     FreeTranslationTable(SendTable);
  1173.  
  1174.                     SendTable = NULL;
  1175.  
  1176.                     FreeTranslationTable(ReceiveTable);
  1177.  
  1178.                     ReceiveTable = NULL;
  1179.                 }
  1180.             }
  1181.             else
  1182.             {
  1183.                 FreeTranslationTable(SendTable);
  1184.  
  1185.                 SendTable = NULL;
  1186.             }
  1187.         }
  1188.     }
  1189.  
  1190.         /* Update the text sending functions. */
  1191.  
  1192.     SendSetup();
  1193.  
  1194.         /* Are we to load the fast macro settings? */
  1195.  
  1196.     if(Config->FastMacroFileName[0] && Stricmp(PrivateConfig->FastMacroFileName,Config->FastMacroFileName))
  1197.     {
  1198.         if(FastWindow)
  1199.             LT_LockWindow(FastWindow);
  1200.  
  1201.         FreeList(&FastMacroList);
  1202.  
  1203.         if(LoadFastMacros(Config->FastMacroFileName,&FastMacroList))
  1204.         {
  1205.             strcpy(LastFastMacros,Config->FastMacroFileName);
  1206.             FastMacroCount = GetListSize(&FastMacroList);
  1207.         }
  1208.  
  1209.         if(FastWindow)
  1210.         {
  1211.             RefreshFastWindow();
  1212.  
  1213.             LT_UnlockWindow(FastWindow);
  1214.         }
  1215.     }
  1216.  
  1217.         /* Serial configuration needs updating? */
  1218.  
  1219.     ReconfigureSerial(Window,NULL);
  1220.  
  1221.         /* Are we to open the fast macro panel? */
  1222.  
  1223.     if(Config->MiscConfig->OpenFastMacroPanel)
  1224.         HadFastMacros = TRUE;
  1225.  
  1226.         /* Are we to freeze the text buffer? */
  1227.  
  1228.     if(!Config->CaptureConfig->BufferEnabled)
  1229.         BufferFrozen = TRUE;
  1230.  
  1231.         /* Now for the actions which require that the
  1232.          * screen stays open.
  1233.          */
  1234.  
  1235.     if(!ResetDisplay)
  1236.     {
  1237.         if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  1238.         {
  1239.             if(PrivateConfig->TerminalConfig->EmulationMode != EMULATION_EXTERNAL || (Config->TerminalConfig->EmulationFileName[0] && strcmp(PrivateConfig->TerminalConfig->EmulationFileName,Config->TerminalConfig->EmulationFileName)))
  1240.             {
  1241.                 if(!OpenEmulator(Config->TerminalConfig->EmulationFileName))
  1242.                 {
  1243.                     Config->TerminalConfig->EmulationMode = EMULATION_ANSIVT100;
  1244.  
  1245.                     ResetDisplay = TRUE;
  1246.  
  1247.                     RasterEnabled = TRUE;
  1248.  
  1249.                     ShowRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Config->TerminalConfig->EmulationFileName);
  1250.                 }
  1251.                 else
  1252.                     RasterEnabled = FALSE;
  1253.             }
  1254.         }
  1255.         else
  1256.         {
  1257.             if(XEmulatorBase && PrivateConfig->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  1258.             {
  1259.                 XEmulatorClearConsole(XEM_IO);
  1260.  
  1261.                 CloseEmulator(FALSE);
  1262.             }
  1263.             else
  1264.                 RasterEnabled = TRUE;
  1265.         }
  1266.  
  1267.         if(RasterEnabled != RasterWasEnabled)
  1268.             RasterEraseScreen(2);
  1269.  
  1270.         if(!Config->ScreenConfig->UseWorkbench && !SharedScreen)
  1271.         {
  1272.             PaletteSetup(Config);
  1273.  
  1274.             LoadColourTable(VPort,NormalColourTable,NormalColours,PaletteSize);
  1275.         }
  1276.  
  1277.         if(Config->MiscConfig->OpenFastMacroPanel && !FastWindow)
  1278.             OpenFastWindow();
  1279.  
  1280.         PubScreenStuff();
  1281.  
  1282.         if(Menu)
  1283.         {
  1284.             CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  1285.  
  1286.             if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0])
  1287.                 OffItem(MEN_CHAT_LINE);
  1288.             else
  1289.                 CheckItem(MEN_CHAT_LINE,ChatMode);
  1290.  
  1291.             SetTransferMenu(TRUE);
  1292.  
  1293.             SetRasterMenu(RasterEnabled);
  1294.  
  1295.             if(MatrixWindow)
  1296.                 CheckItem(MEN_MATRIX_WINDOW,TRUE);
  1297.  
  1298.             if(InfoWindow)
  1299.                 CheckItem(MEN_STATUS_WINDOW,TRUE);
  1300.  
  1301.             if(PacketWindow)
  1302.                 CheckItem(MEN_PACKET_WINDOW,TRUE);
  1303.  
  1304.             if(ChatMode)
  1305.                 CheckItem(MEN_CHAT_LINE,TRUE);
  1306.  
  1307.             if(FastWindow)
  1308.                 CheckItem(MEN_FAST_MACROS_WINDOW,TRUE);
  1309.  
  1310.                 /* Disable the dialing functions if online. */
  1311.  
  1312.             if(Online)
  1313.                 SetDialMenu(FALSE);
  1314.             else
  1315.                 SetDialMenu(TRUE);
  1316.         }
  1317.  
  1318.         Blocking = FALSE;
  1319.     }
  1320.     else
  1321.     {
  1322.             /* Are we no longer to use the external emulator? */
  1323.  
  1324.         if(Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL)
  1325.         {
  1326.             if(XEmulatorBase && PrivateConfig->TerminalConfig->EmulationMode == EMULATION_EXTERNAL)
  1327.             {
  1328.                 XEmulatorClearConsole(XEM_IO);
  1329.  
  1330.                 CloseEmulator(FALSE);
  1331.             }
  1332.         }
  1333.  
  1334.         RasterEnabled = TRUE;
  1335.         FixScreenSize = FALSE;
  1336.     }
  1337.  
  1338.         /* Change the task priority. */
  1339.  
  1340.     SetTaskPri((struct Task *)ThisProcess,(LONG)Config->MiscConfig->Priority);
  1341.  
  1342.     ConOutputUpdate();
  1343.  
  1344.     ConFontScaleUpdate();
  1345.  
  1346.     ConProcessUpdate();
  1347.  
  1348.         /* Reset the scanner. */
  1349.  
  1350.     ResetDataFlowFilter();
  1351.  
  1352.     if(ResetDisplay)
  1353.         ActivateJob(MainJobQueue,ResetDisplayJob);
  1354. }
  1355.  
  1356.     /* DisplayReset():
  1357.      *
  1358.      *    Reset the entire display if necessary.
  1359.      */
  1360.  
  1361. BOOL
  1362. DisplayReset()
  1363. {
  1364.     BOOL Success = TRUE;
  1365.     STRPTR Result;
  1366.  
  1367.         /* Delete the display (if possible).
  1368.          * This will go wrong if there
  1369.          * are any visitor windows on our
  1370.          * screen.
  1371.          */
  1372.  
  1373.     if(DeleteDisplay())
  1374.     {
  1375.         if(Result = CreateDisplay(FALSE,FALSE))
  1376.         {
  1377.             DeleteDisplay();
  1378.  
  1379.             ShowRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Result);
  1380.  
  1381.             Success = FALSE;
  1382.         }
  1383.         else
  1384.         {
  1385.             BumpWindow(Window);
  1386.  
  1387.             PubScreenStuff();
  1388.         }
  1389.     }
  1390.     else
  1391.     {
  1392.         SaveConfig(PrivateConfig,Config);
  1393.  
  1394.         BlockWindows();
  1395.         ShowRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),LocaleString(MSG_TERMMAIN_CANNOT_CLOSE_SCREEN_YET_TXT));
  1396.         ReleaseWindows();
  1397.     }
  1398.  
  1399.     ResetDisplay = FALSE;
  1400.     SuspendJob(MainJobQueue,ResetDisplayJob);
  1401.  
  1402.         /* Prepare for the worst case... */
  1403.  
  1404.     if(!Success)
  1405.         MainTerminated = TRUE;
  1406.  
  1407.     return(Success);
  1408. }
  1409.  
  1410.     /* DeleteDisplay():
  1411.      *
  1412.      *    Free all resources associated with the terminal
  1413.      *    display (tasks, interrupts, screen, window, etc.).
  1414.      */
  1415.  
  1416. BOOL
  1417. DeleteDisplay()
  1418. {
  1419.     struct Screen *WhichScreen;
  1420.  
  1421.     if(SharedScreen)
  1422.         WhichScreen = SharedScreen;
  1423.     else
  1424.         WhichScreen = Screen;
  1425.  
  1426.     if(WhichScreen)
  1427.     {
  1428.         if(!(PubScreenStatus(WhichScreen,PSNF_PRIVATE) & PSNF_PRIVATE))
  1429.             return(FALSE);
  1430.     }
  1431.  
  1432.     StopTerminalTest();
  1433.  
  1434.     GuideCleanup();
  1435.  
  1436.     CloseQueueWindow();
  1437.  
  1438.     ShakeHands((struct Task *)StatusProcess,SIG_KILL);
  1439.  
  1440.     WindowMarkerStop();
  1441.  
  1442.     CloseInfoWindow();
  1443.  
  1444.     DeleteReview();
  1445.  
  1446.     DeleteEmulationProcess();
  1447.  
  1448.     CloseEmulator(FALSE);
  1449.  
  1450.     DeleteRaster();
  1451.  
  1452.     DeleteScale();
  1453.  
  1454.     FreeVecPooled(TabStops);
  1455.     TabStops = NULL;
  1456.  
  1457.     FreeVecPooled(ScrollLines);
  1458.     ScrollLines = NULL;
  1459.  
  1460.     if(Screen)
  1461.         ScreenToBack(Screen);
  1462.  
  1463.     if(FastWindow)
  1464.     {
  1465.         HadFastMacros = TRUE;
  1466.  
  1467.         CloseFastWindow();
  1468.     }
  1469.     else
  1470.         HadFastMacros = FALSE;
  1471.  
  1472.     if(MatrixWindow)
  1473.         CloseMatrixWindow();
  1474.  
  1475.     if(StatusWindow)
  1476.     {
  1477.         ClearMenuStrip(StatusWindow);
  1478.         CloseWindowSafely(StatusWindow);
  1479.  
  1480.         StatusWindow = NULL;
  1481.     }
  1482.  
  1483.     if(DrawInfo)
  1484.     {
  1485.             /* Release the rendering pens. */
  1486.  
  1487.         FreeScreenDrawInfo(Window->WScreen,DrawInfo);
  1488.  
  1489.         DrawInfo = NULL;
  1490.     }
  1491.  
  1492.     HideChatGadget();
  1493.  
  1494.     DeletePacketWindow(FALSE);
  1495.  
  1496.     if(Window)
  1497.     {
  1498.         if(!Screen)
  1499.             PutWindowInfo(WINDOW_MAIN,Window->LeftEdge,Window->TopEdge,Window->Width,Window->Height);
  1500.  
  1501.         if(AllocatedPens && Kick30)
  1502.         {
  1503.             LONG i;
  1504.  
  1505.             ObtainSemaphore(&TerminalSemaphore);
  1506.  
  1507.                 /* Erase the window contents. We will
  1508.                  * want to release any pens we have
  1509.                  * allocated and want to avoid nasty
  1510.                  * flashing and flickering.
  1511.                  */
  1512.  
  1513.             SetAPen(RPort,0);
  1514.  
  1515.             FillBox(RPort,WindowLeft,WindowTop,WindowWidth,WindowHeight);
  1516.  
  1517.                 /* Release any pens we have allocated. */
  1518.  
  1519.             for(i = 0 ; i < 32 ; i++)
  1520.             {
  1521.                 if(MappedPens[1][i])
  1522.                 {
  1523.                     ReleasePen(VPort->ColorMap,MappedPens[0][i]);
  1524.  
  1525.                     MappedPens[0][i] = i;
  1526.                     MappedPens[1][i] = FALSE;
  1527.                 }
  1528.             }
  1529.  
  1530.             AllocatedPens = FALSE;
  1531.  
  1532.             ReleaseSemaphore(&TerminalSemaphore);
  1533.         }
  1534.  
  1535.         if(ClipRegion)
  1536.         {
  1537.             InstallClipRegion(Window->WLayer,OldRegion);
  1538.  
  1539.             DisposeRegion(ClipRegion);
  1540.  
  1541.             ClipRegion = NULL;
  1542.         }
  1543.  
  1544.         ClearMenuStrip(Window);
  1545.  
  1546.         RestoreWindowPtr(OldWindowPtr);
  1547.  
  1548.         PopWindow();
  1549.  
  1550.         if(TermPort)
  1551.             TermPort->TopWindow = NULL;
  1552.  
  1553.         LT_DeleteWindowLock(Window);
  1554.  
  1555.         CloseWindow(Window);
  1556.  
  1557.         Window = NULL;
  1558.     }
  1559.  
  1560.     if(Menu)
  1561.     {
  1562.         LT_DisposeMenu(Menu);
  1563.         Menu = NULL;
  1564.     }
  1565.  
  1566.     FreeVisualInfo(VisualInfo);
  1567.     VisualInfo = NULL;
  1568.  
  1569.         /* Clean up the menu glyphs. */
  1570.  
  1571.     DisposeObject(AmigaGlyph);
  1572.  
  1573.     AmigaGlyph = NULL;
  1574.  
  1575.     DisposeObject(CheckGlyph);
  1576.  
  1577.     CheckGlyph = NULL;
  1578.  
  1579.     if(Screen)
  1580.     {
  1581.         CloseScreen(Screen);
  1582.  
  1583.         Screen = NULL;
  1584.     }
  1585.  
  1586.     if(SharedScreen)
  1587.     {
  1588.         CloseScreen(SharedScreen);
  1589.  
  1590.         SharedScreen = NULL;
  1591.     }
  1592.  
  1593.     ReleaseDefaultPubScreen();
  1594.  
  1595.     if(GFX)
  1596.     {
  1597.         CloseFont(GFX);
  1598.  
  1599.         GFX = NULL;
  1600.     }
  1601.  
  1602.     if(TextFont)
  1603.     {
  1604.         CloseFont(TextFont);
  1605.  
  1606.         TextFont = NULL;
  1607.     }
  1608.  
  1609.     if(UserTextFont)
  1610.     {
  1611.         CloseFont(UserTextFont);
  1612.  
  1613.         UserTextFont = NULL;
  1614.     }
  1615.  
  1616.     return(TRUE);
  1617. }
  1618.  
  1619.     /* CreateDisplay(BOOL UsePresetSize):
  1620.      *
  1621.      *    Open the display and allocate associated data.
  1622.      */
  1623.  
  1624. STRPTR
  1625. CreateDisplay(BOOL UsePresetSize,BOOL Activate)
  1626. {
  1627.     LONG ErrorCode,Top,Height,Count,RealDepth,i;
  1628.     LONG StatusWidth,StatusHeight;
  1629.     struct Rectangle DisplayClip;
  1630.     UWORD PenArray[16];
  1631.     ULONG X_DPI,Y_DPI;
  1632.     ULONG TagArray[9];
  1633.     BOOL OpenFailed;
  1634.     BOOL RethinkPens;
  1635.  
  1636.     OpenFailed = FALSE;
  1637.     Count = 0;
  1638.  
  1639.     ResetDisplay = FALSE;
  1640.     SuspendJob(MainJobQueue,ResetDisplayJob);
  1641.  
  1642.     BlockNestCount = 0;
  1643.  
  1644.     WeAreBlocking = FALSE;
  1645.  
  1646.     SetQueueDiscard(SpecialQueue,FALSE);
  1647.  
  1648.     TagDPI[0].ti_Tag = TAG_DONE;
  1649.  
  1650.         /* Don't permit weird settings. */
  1651.  
  1652.     if(!Config->ScreenConfig->StatusLine || (!Config->ScreenConfig->ShareScreen && !Config->ScreenConfig->UseWorkbench))
  1653.         Config->ScreenConfig->SplitStatus = FALSE;
  1654.  
  1655.     if(Config->ScreenConfig->UseWorkbench || Config->ScreenConfig->ShareScreen)
  1656.     {
  1657.         STRPTR ScreenName = NULL;
  1658.  
  1659.         if(Config->ScreenConfig->PubScreenName[0])
  1660.         {
  1661.             struct Screen *SomeScreen;
  1662.  
  1663.             if(SomeScreen = LockPubScreen(Config->ScreenConfig->PubScreenName))
  1664.             {
  1665.                 UnlockPubScreen(NULL,SomeScreen);
  1666.  
  1667.                 ScreenName = Config->ScreenConfig->PubScreenName;
  1668.             }
  1669.         }
  1670.  
  1671.         if(!(DefaultPubScreen = LockPubScreen(ScreenName)))
  1672.             return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_DEFAULT_PUBLIC_SCREEN_TXT));
  1673.         else
  1674.         {
  1675.             GetDPI(GetVPModeID(&DefaultPubScreen->ViewPort),&X_DPI,&Y_DPI);
  1676.  
  1677.             strcpy(UserFontName,DefaultPubScreen->Font->ta_Name);
  1678.  
  1679.             UserFont.tta_Name    = UserFontName;
  1680.             UserFont.tta_YSize    = DefaultPubScreen->Font->ta_YSize;
  1681.             UserFont.tta_Style    = DefaultPubScreen->Font->ta_Style;
  1682.             UserFont.tta_Flags    = DefaultPubScreen->Font->ta_Flags;
  1683.         }
  1684.     }
  1685.  
  1686.     if(!Config->ScreenConfig->UseWorkbench)
  1687.     {
  1688.         GetDPI(Config->ScreenConfig->DisplayMode,&X_DPI,&Y_DPI);
  1689.  
  1690.         strcpy(UserFontName,Config->ScreenConfig->FontName);
  1691.  
  1692.         UserFont.tta_Name    = UserFontName;
  1693.         UserFont.tta_YSize    = Config->ScreenConfig->FontHeight;
  1694.         UserFont.tta_Style    = FS_NORMAL | FSF_TAGGED;
  1695.         UserFont.tta_Flags    = FPF_DESIGNED;
  1696.         UserFont.tta_Tags    = TagDPI;
  1697.  
  1698.         TagDPI[0].ti_Tag     = TA_DeviceDPI;
  1699.         TagDPI[0].ti_Data     = (X_DPI << 16) | Y_DPI;
  1700.         TagDPI[1].ti_Tag     = TAG_DONE;
  1701.     }
  1702.  
  1703.     if(!(UserTextFont = SmartOpenDiskFont((struct TextAttr *)&UserFont)))
  1704.     {
  1705.         if(Config->ScreenConfig->UseWorkbench)
  1706.         {
  1707.             ReleaseDefaultPubScreen();
  1708.  
  1709.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1710.         }
  1711.         else
  1712.         {
  1713.             strcpy(Config->ScreenConfig->FontName,    "topaz.font");
  1714.             strcpy(UserFontName,                "topaz.font");
  1715.  
  1716.             Config->ScreenConfig->FontHeight = 8;
  1717.  
  1718.             UserFont.tta_YSize    = Config->ScreenConfig->FontHeight;
  1719.             UserFont.tta_Style    = FS_NORMAL;
  1720.             UserFont.tta_Flags    = FPF_DESIGNED | FPF_ROMFONT;
  1721.  
  1722.             if(!(UserTextFont = OpenFont((struct TextAttr *)&UserFont)))
  1723.             {
  1724.                 ReleaseDefaultPubScreen();
  1725.  
  1726.                 return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_FONT_TXT));
  1727.             }
  1728.         }
  1729.     }
  1730.  
  1731.         /* Open the default text rendering font. */
  1732.  
  1733.     do
  1734.     {
  1735.             /* If it's not the standard font, use the IBM PC style font. */
  1736.  
  1737.         if(Config->TerminalConfig->FontMode != FONT_STANDARD)
  1738.         {
  1739.             strcpy(TextFontName,Config->TerminalConfig->IBMFontName);
  1740.  
  1741.             TextAttr.tta_YSize = Config->TerminalConfig->IBMFontHeight;
  1742.         }
  1743.         else
  1744.         {
  1745.             strcpy(TextFontName,Config->TerminalConfig->TextFontName);
  1746.  
  1747.             TextAttr.tta_YSize = Config->TerminalConfig->TextFontHeight;
  1748.         }
  1749.  
  1750.         TextAttr.tta_Name    = TextFontName;
  1751.         TextAttr.tta_Style    = FS_NORMAL | FSF_TAGGED;
  1752.         TextAttr.tta_Flags    = FPF_DESIGNED;
  1753.         TextAttr.tta_Tags    = TagDPI;
  1754.  
  1755.             /* Does it open? */
  1756.  
  1757.         if(!(TextFont = SmartOpenDiskFont((struct TextAttr *)&TextAttr)))
  1758.         {
  1759.                 /* So it didn't open. Revert to the standard text font. */
  1760.  
  1761.             if(Config->TerminalConfig->FontMode != FONT_STANDARD)
  1762.                 Config->TerminalConfig->FontMode = FONT_STANDARD;
  1763.             else
  1764.             {
  1765.                     /* So this is the standard font. Is it the ROM default font? */
  1766.  
  1767.                 if(!Stricmp(Config->TerminalConfig->TextFontName,"topaz.font") && Config->TerminalConfig->TextFontHeight == 8)
  1768.                 {
  1769.                         /* Looks like you lose. */
  1770.  
  1771.                     ReleaseDefaultPubScreen();
  1772.  
  1773.                     return(LocaleString(MSG_TERMINIT_UNABLE_TO_OPEN_TEXT_TXT));
  1774.                 }
  1775.                 else
  1776.                 {
  1777.                         /* Use the ROM default font. */
  1778.  
  1779.                     strcpy(Config->TerminalConfig->TextFontName,"topaz.font");
  1780.                     Config->TerminalConfig->TextFontHeight = 8;
  1781.                 }
  1782.             }
  1783.         }
  1784.     }
  1785.     while(TextFont == NULL);
  1786.  
  1787.     TextFontHeight    = TextFont->tf_YSize;
  1788.     TextFontWidth    = TextFont->tf_XSize;
  1789.     TextFontBase    = TextFont->tf_Baseline;
  1790.  
  1791.         /* Determine extra font box width for slanted/boldface glyphs. */
  1792.  
  1793.     FontRightExtend    = MAX(TextFont->tf_XSize / 2,TextFont->tf_BoldSmear);
  1794.  
  1795.     CurrentFont = TextFont;
  1796.  
  1797.     GFXFont.ta_YSize = TextFontHeight;
  1798.  
  1799.     if(GFX = SmartOpenDiskFont(&GFXFont))
  1800.     {
  1801.         if(GFX->tf_XSize != TextFont->tf_XSize || GFX->tf_YSize != TextFont->tf_YSize)
  1802.         {
  1803.             CloseFont(GFX);
  1804.  
  1805.             GFX = NULL;
  1806.         }
  1807.     }
  1808.  
  1809.     UserFontHeight    = UserTextFont->tf_YSize;
  1810.     UserFontWidth    = UserTextFont->tf_XSize;
  1811.     UserFontBase    = UserTextFont->tf_Baseline;
  1812.  
  1813.         /* We will need this one later */
  1814.  
  1815.     OpenWindowTag = WA_CustomScreen;
  1816.  
  1817.     if(Config->ScreenConfig->UseWorkbench || Config->ScreenConfig->ShareScreen)
  1818.     {
  1819.         LONG WindowLeft,WindowTop,DummyWidth,DummyHeight;
  1820.         LONG FullWidth,Height,Width,Index;
  1821.         struct ViewPortExtra *Extra;
  1822.         struct TagItem SomeTags[7];
  1823.         struct Screen *LocalScreen;
  1824.  
  1825.         Index = 0;
  1826.         LocalScreen = DefaultPubScreen;
  1827.         WindowLeft = WindowTop = DummyWidth = DummyHeight = -1;
  1828.  
  1829.         if(Config->ScreenConfig->ShareScreen && !Config->ScreenConfig->UseWorkbench)
  1830.         {
  1831.             struct DimensionInfo DimensionInfo;
  1832.  
  1833.             if(ModeNotAvailable(Config->ScreenConfig->DisplayMode))
  1834.             {
  1835.                 Config->ScreenConfig->DisplayMode = GetVPModeID(&DefaultPubScreen->ViewPort);
  1836.  
  1837.                 if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  1838.                 {
  1839.                     LONG Width,Height;
  1840.  
  1841.                     Width    = DimensionInfo.TxtOScan.MaxX - DimensionInfo.TxtOScan.MinX + 1;
  1842.                     Height    = DimensionInfo.TxtOScan.MaxY - DimensionInfo.TxtOScan.MinY + 1;
  1843.  
  1844.                     if(Width != Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayWidth)
  1845.                         Config->ScreenConfig->DisplayWidth = Width;
  1846.  
  1847.                     if(Height != Config->ScreenConfig->DisplayHeight && Config->ScreenConfig->DisplayHeight)
  1848.                         Config->ScreenConfig->DisplayHeight = Height;
  1849.                 }
  1850.             }
  1851.  
  1852.             if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  1853.             {
  1854.                 LONG Depth,ScreenWidth,ScreenHeight;
  1855.                 UWORD Pens;
  1856.  
  1857.                 Pens = (UWORD)~0;
  1858.  
  1859.                 if(Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayHeight && AslBase->lib_Version >= 38)
  1860.                 {
  1861.                     ScreenWidth        = Config->ScreenConfig->DisplayWidth;
  1862.                     ScreenHeight    = Config->ScreenConfig->DisplayHeight;
  1863.                 }
  1864.                 else
  1865.                 {
  1866.                     ScreenWidth        = 0;
  1867.                     ScreenHeight    = 0;
  1868.                 }
  1869.  
  1870.                 switch(Config->ScreenConfig->ColourMode)
  1871.                 {
  1872.                     case COLOUR_EIGHT:
  1873.  
  1874.                         Depth = 3 + 1;
  1875.                         break;
  1876.  
  1877.                     case COLOUR_SIXTEEN:
  1878.  
  1879.                         Depth = 4 + 1;
  1880.                         break;
  1881.  
  1882.                     case COLOUR_AMIGA:
  1883.  
  1884.                         Depth = 2;
  1885.                         break;
  1886.  
  1887.                     default:
  1888.  
  1889.                         Depth = 1;
  1890.                         break;
  1891.                 }
  1892.  
  1893.                 if(Depth > DimensionInfo.MaxDepth)
  1894.                     Depth = DimensionInfo.MaxDepth;
  1895.  
  1896.                 if(!Kick30 && Depth > 2)
  1897.                     Depth = 2;
  1898.  
  1899.                 if(Config->ScreenConfig->Depth && Config->ScreenConfig->Depth <= DimensionInfo.MaxDepth)
  1900.                     Depth = Config->ScreenConfig->Depth;
  1901.  
  1902.                 switch(Config->ScreenConfig->ColourMode)
  1903.                 {
  1904.                     case COLOUR_EIGHT:
  1905.  
  1906.                         if(Depth < 3 + 1)
  1907.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  1908.  
  1909.                         break;
  1910.  
  1911.                     case COLOUR_SIXTEEN:
  1912.  
  1913.                         if(Depth < 4 + 1)
  1914.                         {
  1915.                             if(Depth < 3 + 1)
  1916.                                 Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  1917.                             else
  1918.                                 Config->ScreenConfig->ColourMode = COLOUR_EIGHT;
  1919.                         }
  1920.  
  1921.                         break;
  1922.                 }
  1923.  
  1924.                 LimitedSPrintf(sizeof(ScreenTitle),ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,Machine,TermDate,TermIDString);
  1925.  
  1926.                 StatusSizeSetup(NULL,&StatusWidth,&StatusHeight);
  1927.  
  1928.                 if(StatusHeight && StatusWidth > ScreenWidth)
  1929.                     ScreenWidth = StatusWidth;
  1930.  
  1931.                 if(SharedScreen = OpenScreenTags(NULL,
  1932.                     ScreenWidth  ? SA_Width  : TAG_IGNORE,    ScreenWidth,
  1933.                     ScreenHeight ? SA_Height : TAG_IGNORE,    ScreenHeight,
  1934.  
  1935.                     SA_Title,        ScreenTitle,
  1936.                     SA_Depth,        Depth,
  1937.                     SA_Pens,        &Pens,
  1938.                     SA_Overscan,    AslBase->lib_Version >= 38 ? Config->ScreenConfig->OverscanType : OSCAN_TEXT,
  1939.                     SA_DisplayID,    Config->ScreenConfig->DisplayMode,
  1940.                     SA_Font,        &UserFont,
  1941.                     SA_AutoScroll,    TRUE,
  1942.                     SA_ShowTitle,    Config->ScreenConfig->TitleBar,
  1943.                     SA_PubName,        TermIDString,
  1944.                     SA_Interleaved,    Config->ScreenConfig->FasterLayout,
  1945.                     SA_SharePens,    TRUE,
  1946.                 TAG_DONE))
  1947.                 {
  1948.                     LocalScreen = SharedScreen;
  1949.  
  1950.                     if(Config->ScreenConfig->MakeScreenPublic)
  1951.                         PubScreenStatus(LocalScreen,NULL);
  1952.                     else
  1953.                         PubScreenStatus(LocalScreen,PSNF_PRIVATE);
  1954.                 }
  1955.             }
  1956.         }
  1957.  
  1958.         if(!SharedScreen)
  1959.             LimitedSPrintf(sizeof(ScreenTitle),ScreenTitle,"%s %s(%s)",TermName,Machine,TermDate);
  1960.  
  1961.         StatusSizeSetup(LocalScreen,&StatusWidth,&StatusHeight);
  1962.  
  1963.         if(GetBitMapDepth(LocalScreen->RastPort.BitMap) == 1 || Config->ScreenConfig->FasterLayout)
  1964.             UseMasking = FALSE;
  1965.         else
  1966.         {
  1967.             if(Kick30)
  1968.             {
  1969.                 if(GetBitMapAttr(LocalScreen->RastPort.BitMap,BMA_FLAGS) & BMF_INTERLEAVED)
  1970.                     UseMasking = FALSE;
  1971.                 else
  1972.                     UseMasking = TRUE;
  1973.             }
  1974.             else
  1975.                 UseMasking = TRUE;
  1976.         }
  1977.  
  1978.         VPort = &LocalScreen->ViewPort;
  1979.  
  1980.             /* Get the current display dimensions. */
  1981.  
  1982.         if(Extra = GetViewPortExtra(VPort))
  1983.         {
  1984.             ScreenWidth        = Extra->DisplayClip.MaxX - Extra->DisplayClip.MinX + 1;
  1985.             ScreenHeight    = Extra->DisplayClip.MaxY - Extra->DisplayClip.MinY + 1;
  1986.         }
  1987.         else
  1988.         {
  1989.             ScreenWidth        = LocalScreen->Width;
  1990.             ScreenHeight    = LocalScreen->Height;
  1991.         }
  1992.  
  1993.         DepthMask = (1L << GetBitMapDepth(LocalScreen->RastPort.BitMap)) - 1;
  1994.  
  1995.         switch(Config->ScreenConfig->ColourMode)
  1996.         {
  1997.             case COLOUR_SIXTEEN:
  1998.  
  1999.                 if(DepthMask < 15)
  2000.                 {
  2001.                     if(DepthMask >= 7)
  2002.                         Config->ScreenConfig->ColourMode = COLOUR_EIGHT;
  2003.                     else
  2004.                     {
  2005.                         if(DepthMask >= 3)
  2006.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2007.                         else
  2008.                             Config->ScreenConfig->ColourMode = COLOUR_MONO;
  2009.                     }
  2010.                 }
  2011.  
  2012.                 break;
  2013.  
  2014.             case COLOUR_EIGHT:
  2015.  
  2016.                 if(DepthMask < 7)
  2017.                 {
  2018.                     if(DepthMask >= 3)
  2019.                         Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2020.                     else
  2021.                         Config->ScreenConfig->ColourMode = COLOUR_MONO;
  2022.                 }
  2023.  
  2024.                 break;
  2025.  
  2026.             case COLOUR_AMIGA:
  2027.  
  2028.                 if(DepthMask < 3)
  2029.                     Config->ScreenConfig->ColourMode = COLOUR_MONO;
  2030.  
  2031.                 break;
  2032.         }
  2033.  
  2034.         if(!(DrawInfo = GetScreenDrawInfo(LocalScreen)))
  2035.         {
  2036.             ReleaseDefaultPubScreen();
  2037.  
  2038.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_SCREEN_DRAWINFO_TXT));
  2039.         }
  2040.         else
  2041.             Pens = DrawInfo->dri_Pens;
  2042.  
  2043.         CreateMenuGlyphs(LocalScreen,DrawInfo,&AmigaGlyph,&CheckGlyph);
  2044.  
  2045.         SZ_SizeSetup(LocalScreen,(struct TextAttr *)&UserFont);
  2046.  
  2047.             /* Obtain visual info (whatever that may be). */
  2048.  
  2049.         if(!(VisualInfo = GetVisualInfo(LocalScreen,TAG_DONE)))
  2050.         {
  2051.                 /* Delete the DrawInfo now, or it won't
  2052.                  * get freed during shutdown.
  2053.                  */
  2054.  
  2055.             FreeScreenDrawInfo(LocalScreen,DrawInfo);
  2056.             DrawInfo = NULL;
  2057.  
  2058.             ReleaseDefaultPubScreen();
  2059.  
  2060.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  2061.         }
  2062.  
  2063.         if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && !Config->ScreenConfig->SplitStatus)
  2064.             FullWidth = StatusWidth;
  2065.         else
  2066.             FullWidth = 0;
  2067.  
  2068.         GetWindowInfo(WINDOW_MAIN,&WindowLeft,&WindowTop,&DummyWidth,&DummyHeight,0,0);
  2069.  
  2070.         if(UsePresetSize)
  2071.         {
  2072.             SomeTags[Index  ].ti_Tag    = WA_Width;
  2073.             SomeTags[Index++].ti_Data    = DummyWidth;
  2074.  
  2075.             SomeTags[Index  ].ti_Tag    = WA_Height;
  2076.             SomeTags[Index++].ti_Data    = DummyHeight;
  2077.         }
  2078.         else
  2079.         {
  2080.             if(Config->TerminalConfig->NumColumns < 20)
  2081.             {
  2082.                 LONG Width = GetScreenWidth(NULL);
  2083.  
  2084.                 if(FullWidth && Width < FullWidth)
  2085.                 {
  2086.                     SomeTags[Index  ].ti_Tag    = WA_InnerWidth;
  2087.                     SomeTags[Index++].ti_Data    = FullWidth;
  2088.                 }
  2089.                 else
  2090.                 {
  2091.                     SomeTags[Index  ].ti_Tag    = WA_Width;
  2092.                     SomeTags[Index++].ti_Data    = Width;
  2093.                 }
  2094.             }
  2095.             else
  2096.             {
  2097.                 SomeTags[Index  ].ti_Tag    = WA_InnerWidth;
  2098.                 SomeTags[Index++].ti_Data    = Config->TerminalConfig->NumColumns * TextFontWidth;
  2099.             }
  2100.  
  2101.             if(Config->TerminalConfig->NumLines < 20)
  2102.             {
  2103.                 SomeTags[Index  ].ti_Tag    = WA_Height;
  2104.                 SomeTags[Index++].ti_Data    = GetScreenHeight(NULL) - (LocalScreen->BarHeight + 1);
  2105.             }
  2106.             else
  2107.             {
  2108.                 SomeTags[Index  ].ti_Tag    = WA_InnerHeight;
  2109.                 SomeTags[Index++].ti_Data    = Config->TerminalConfig->NumLines * TextFontHeight + StatusHeight;
  2110.             }
  2111.         }
  2112.  
  2113.         if(WindowLeft != -1)
  2114.         {
  2115.             SomeTags[Index  ].ti_Tag    = WA_Left;
  2116.             SomeTags[Index++].ti_Data    = WindowLeft;
  2117.         }
  2118.         else
  2119.         {
  2120.             SomeTags[Index  ].ti_Tag    = WA_Left;
  2121.             SomeTags[Index++].ti_Data    = GetScreenLeft(NULL);
  2122.         }
  2123.  
  2124.         if(WindowTop != -1)
  2125.         {
  2126.             SomeTags[Index  ].ti_Tag    = WA_Top;
  2127.             SomeTags[Index++].ti_Data    = WindowTop;
  2128.         }
  2129.  
  2130.         SomeTags[Index].ti_Tag = TAG_DONE;
  2131.  
  2132.             /* If we're using a public screen, take care of it. */
  2133.  
  2134.         if(!SharedScreen)
  2135.             OpenWindowTag = WA_PubScreen;
  2136.  
  2137.             /* Open the main window. */
  2138.  
  2139.         if(!(Window = OpenWindowTags(NULL,
  2140.             WA_MaxHeight,        LocalScreen->Height,
  2141.             WA_MaxWidth,        LocalScreen->Width,
  2142.             WA_SmartRefresh,    TRUE,
  2143.             WA_NewLookMenus,    TRUE,
  2144.             WA_RMBTrap,            TRUE,
  2145.             WA_IDCMP,            DEFAULT_IDCMP | IDCMP_SIZEVERIFY,
  2146.             WA_DragBar,            TRUE,
  2147.             WA_DepthGadget,        TRUE,
  2148.             WA_CloseGadget,        TRUE,
  2149.             WA_SizeGadget,        TRUE,
  2150.             WA_SizeBBottom,        Config->ScreenConfig->StatusLine == STATUSLINE_DISABLED || Config->ScreenConfig->SplitStatus,
  2151.             WA_NoCareRefresh,    TRUE,
  2152.             WA_Title,            WindowTitle[0] ? WindowTitle : ScreenTitle,
  2153.             WA_MenuHelp,        TRUE,
  2154.             WA_Activate,        Activate,
  2155.             OpenWindowTag,        LocalScreen,
  2156.  
  2157.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  2158.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  2159.  
  2160.         TAG_MORE,SomeTags)))
  2161.         {
  2162.                 /* Delete the DrawInfo now, or it won't
  2163.                  * get freed during shutdown.
  2164.                  */
  2165.  
  2166.             FreeScreenDrawInfo(LocalScreen,DrawInfo);
  2167.             DrawInfo = NULL;
  2168.  
  2169.             ReleaseDefaultPubScreen();
  2170.  
  2171.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2172.         }
  2173.  
  2174.         if(WindowTitle[0])
  2175.             SetWindowTitles(Window,(STRPTR)-1,ScreenTitle);
  2176.  
  2177.         if(StatusHeight)
  2178.         {
  2179.             StatusDisplayHeight = StatusHeight;
  2180.  
  2181.             CopyMem(Window->RPort,StatusRPort = &StatusRastPort,sizeof(struct RastPort));
  2182.         }
  2183.         else
  2184.             StatusRPort = NULL;
  2185.  
  2186.             /* Create a user clip region to keep text from
  2187.              * leaking into the window borders.
  2188.              */
  2189.  
  2190.         if(!(ClipRegion = NewRegion()))
  2191.         {
  2192.             ReleaseDefaultPubScreen();
  2193.  
  2194.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2195.         }
  2196.         else
  2197.         {
  2198.             struct Rectangle RegionRectangle;
  2199.  
  2200.                 /* Adjust the region to match the inner window area. */
  2201.  
  2202.             RegionRectangle.MinX = Window->BorderLeft;
  2203.             RegionRectangle.MinY = Window->BorderTop;
  2204.             RegionRectangle.MaxX = Window->Width - (Window->BorderRight + 1);
  2205.             RegionRectangle.MaxY = Window->Height - (Window->BorderBottom + 1);
  2206.  
  2207.                 /* Establish the region. */
  2208.  
  2209.             OrRectRegion(ClipRegion,&RegionRectangle);
  2210.  
  2211.                 /* Install the region. */
  2212.  
  2213.             OldRegion = InstallClipRegion(Window->WLayer,ClipRegion);
  2214.         }
  2215.  
  2216.         if(FullWidth < 40 * TextFontWidth)
  2217.             FullWidth = 40 * TextFontWidth;
  2218.  
  2219.         Width    = Window->BorderLeft + FullWidth + Window->BorderRight;
  2220.         Height    = Window->BorderTop + 20 * TextFontHeight + Window->BorderBottom + StatusHeight;
  2221.  
  2222.         if(ChatMode && !(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0]))
  2223.             Height += UserFontHeight + 2;
  2224.  
  2225.         WindowLimits(Window,Width,Height,0,0);
  2226.  
  2227.         ReleaseDefaultPubScreen();
  2228.     }
  2229.     else
  2230.     {
  2231.         struct DimensionInfo DimensionInfo;
  2232.         LONG MaxDepth,ScreenDepth;
  2233.  
  2234.         if(ModeNotAvailable(Config->ScreenConfig->DisplayMode))
  2235.         {
  2236.             struct Screen *PubScreen;
  2237.  
  2238.             if(PubScreen = LockPubScreen(NULL))
  2239.             {
  2240.                 Config->ScreenConfig->DisplayMode = GetVPModeID(&PubScreen->ViewPort);
  2241.  
  2242.                 if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  2243.                 {
  2244.                     LONG Width,Height;
  2245.  
  2246.                     Width    = DimensionInfo.TxtOScan.MaxX - DimensionInfo.TxtOScan.MinX + 1;
  2247.                     Height    = DimensionInfo.TxtOScan.MaxY - DimensionInfo.TxtOScan.MinY + 1;
  2248.  
  2249.                     if(Width != Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayWidth)
  2250.                         Config->ScreenConfig->DisplayWidth = Width;
  2251.  
  2252.                     if(Height != Config->ScreenConfig->DisplayHeight && Config->ScreenConfig->DisplayHeight)
  2253.                         Config->ScreenConfig->DisplayHeight = Height;
  2254.                 }
  2255.  
  2256.                 UnlockPubScreen(NULL,PubScreen);
  2257.             }
  2258.         }
  2259.  
  2260.             /* Query the display size. If it fails, back out immediately. */
  2261.  
  2262.         if(!QueryOverscan(Config->ScreenConfig->DisplayMode,&DisplayClip,Config->ScreenConfig->OverscanType))
  2263.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  2264.  
  2265.         if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  2266.         {
  2267.             LONG MaxWidth,MaxHeight,Width,Height;
  2268.  
  2269.             MaxWidth    = DisplayClip.MaxX - DisplayClip.MinX + 1;
  2270.             MaxHeight    = DisplayClip.MaxY - DisplayClip.MinY + 1;
  2271.  
  2272.             if(Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayHeight && AslBase->lib_Version >= 38)
  2273.             {
  2274.                 ScreenWidth        = Config->ScreenConfig->DisplayWidth;
  2275.                 ScreenHeight    = Config->ScreenConfig->DisplayHeight;
  2276.             }
  2277.             else
  2278.             {
  2279.                 ScreenWidth        = MaxWidth;
  2280.                 ScreenHeight    = MaxHeight;
  2281.             }
  2282.  
  2283.             if(Config->TerminalConfig->NumColumns < 20)
  2284.                 Width = MaxWidth = ScreenWidth;
  2285.             else
  2286.             {
  2287.                 Width = TextFontWidth * Config->TerminalConfig->NumColumns;
  2288.  
  2289.                 ScreenWidth = 0;
  2290.             }
  2291.  
  2292.             if(Config->TerminalConfig->NumLines < 20)
  2293.                 Height = MaxHeight = ScreenHeight;
  2294.             else
  2295.             {
  2296.                 Height = TextFontHeight * Config->TerminalConfig->NumLines;
  2297.  
  2298.                 if(Config->ScreenConfig->TitleBar)
  2299.                     Height += UserFontHeight + 3;
  2300.  
  2301.                 if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED)
  2302.                 {
  2303.                     if(Config->ScreenConfig->StatusLine == STATUSLINE_COMPRESSED)
  2304.                         Height += UserFontHeight;
  2305.                     else
  2306.                         Height += 4 + (2 + 2 * UserFontHeight + 2);
  2307.                 }
  2308.  
  2309.                 ScreenHeight = 0;
  2310.             }
  2311.  
  2312.             if(Height > MaxHeight)
  2313.                 Height = MaxHeight;
  2314.  
  2315.             if(Width > MaxWidth)
  2316.                 Width = MaxWidth;
  2317.  
  2318.             if(DimensionInfo.MinRasterWidth <= Width && Width <= DimensionInfo.MaxRasterWidth && Width < MaxWidth)
  2319.             {
  2320.                 LONG Half;
  2321.  
  2322.                 Width = MaxWidth - Width;
  2323.  
  2324.                 Half = Width / 2;
  2325.  
  2326.                 DisplayClip.MinX += Half;
  2327.                 DisplayClip.MaxX -= Width - Half;
  2328.             }
  2329.  
  2330.             if(DimensionInfo.MinRasterHeight <= Height && Height <= DimensionInfo.MaxRasterHeight)
  2331.                 DisplayClip.MaxY = DisplayClip.MinY + Height - 1;
  2332.  
  2333.             if(!ScreenWidth)
  2334.                 ScreenWidth = DisplayClip.MaxX - DisplayClip.MinX + 1;
  2335.  
  2336.             if(!ScreenHeight)
  2337.                 ScreenHeight = DisplayClip.MaxY - DisplayClip.MinY + 1;
  2338.  
  2339.             MaxDepth = DimensionInfo.MaxDepth;
  2340.         }
  2341.         else
  2342.         {
  2343.             ScreenWidth = ScreenHeight = 0;
  2344.             MaxDepth = 4;
  2345.         }
  2346.  
  2347.             /* We'll configure the screen parameters at
  2348.              * run time, at first we'll set up the screen
  2349.              * depth.
  2350.              */
  2351.  
  2352.         do
  2353.         {
  2354.             RethinkPens = FALSE;
  2355.  
  2356.             if(!Config->ScreenConfig->UsePens && Kick30)
  2357.             {
  2358.                 for(i = DETAILPEN ; i <= BARTRIMPEN ; i++)
  2359.                     PenArray[i] = Config->ScreenConfig->PenArray[i];
  2360.  
  2361.                 PenArray[i] = (UWORD)~0;
  2362.             }
  2363.             else
  2364.             {
  2365.                 UWORD *Data;
  2366.  
  2367.                 switch(Config->ScreenConfig->ColourMode)
  2368.                 {
  2369.                     case COLOUR_EIGHT:
  2370.  
  2371.                         Data = ANSIPens;
  2372.                         break;
  2373.  
  2374.                     case COLOUR_SIXTEEN:
  2375.  
  2376.                         if(Kick30)
  2377.                             Data = NewEGAPens;
  2378.                         else
  2379.                             Data = EGAPens;
  2380.  
  2381.                         break;
  2382.  
  2383.                     case COLOUR_AMIGA:
  2384.  
  2385.                         Data = StandardPens;
  2386.                         break;
  2387.  
  2388.                     default:
  2389.  
  2390.                         Data = NULL;
  2391.                         break;
  2392.                 }
  2393.  
  2394.                 if(Data)
  2395.                 {
  2396.                     for(i = DETAILPEN ; i <= BARTRIMPEN ; i++)
  2397.                         PenArray[i] = Data[i];
  2398.  
  2399.                     PenArray[i] = (UWORD)~0;
  2400.                 }
  2401.             }
  2402.  
  2403.             switch(Config->ScreenConfig->ColourMode)
  2404.             {
  2405.                 case COLOUR_EIGHT:
  2406.  
  2407.                         /* Special screen depth requested? */
  2408.  
  2409.                     if(Config->ScreenConfig->Depth)
  2410.                     {
  2411.                             /* The minimum number of colours required */
  2412.  
  2413.                         if(Config->ScreenConfig->Blinking)
  2414.                             ScreenDepth = 4;
  2415.                         else
  2416.                             ScreenDepth = 3;
  2417.  
  2418.                             /* This is what the user wanted */
  2419.  
  2420.                         RealDepth = Config->ScreenConfig->Depth;
  2421.  
  2422.                             /* Too deep for this display mode? */
  2423.  
  2424.                         if(RealDepth > MaxDepth)
  2425.                             RealDepth = MaxDepth;
  2426.  
  2427.                             /* Less colours than required? */
  2428.  
  2429.                         if(RealDepth < ScreenDepth && ScreenDepth <= MaxDepth)
  2430.                             RealDepth = ScreenDepth;
  2431.  
  2432.                             /* Not enough colours to display it? */
  2433.  
  2434.                         if(RealDepth < ScreenDepth)
  2435.                         {
  2436.                                 /* Return to standard mode */
  2437.  
  2438.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2439.  
  2440.                             ConfigChanged = TRUE;
  2441.  
  2442.                             RethinkPens = TRUE;
  2443.  
  2444.                             break;
  2445.                         }
  2446.                     }
  2447.                     else
  2448.                     {
  2449.                             /* The minimum number of colours */
  2450.  
  2451.                         if(Config->ScreenConfig->Blinking)
  2452.                             ScreenDepth = 4;
  2453.                         else
  2454.                             ScreenDepth = 3;
  2455.  
  2456.                             /* Too many for this mode? */
  2457.  
  2458.                         if(ScreenDepth > MaxDepth)
  2459.                         {
  2460.                                 /* Return to standard mode */
  2461.  
  2462.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2463.  
  2464.                             ConfigChanged = TRUE;
  2465.                             RethinkPens = TRUE;
  2466.  
  2467.                             break;
  2468.                         }
  2469.  
  2470.                         RealDepth = ScreenDepth;
  2471.                     }
  2472.  
  2473.                     TagArray[Count++] = SA_Pens;
  2474.                     TagArray[Count++] = (LONG)PenArray;
  2475.  
  2476.                     TagArray[Count++] = SA_BlockPen;
  2477.                     TagArray[Count++] = PenArray[SHADOWPEN];
  2478.  
  2479.                     TagArray[Count++] = SA_DetailPen;
  2480.                     TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2481.  
  2482.                     break;
  2483.  
  2484.                 case COLOUR_SIXTEEN:
  2485.  
  2486.                     if(Config->ScreenConfig->Depth)
  2487.                     {
  2488.                         if(Config->ScreenConfig->Blinking && MaxDepth > 4)
  2489.                             ScreenDepth = 5;
  2490.                         else
  2491.                             ScreenDepth = 4;
  2492.  
  2493.                         RealDepth = Config->ScreenConfig->Depth;
  2494.  
  2495.                         if(RealDepth > MaxDepth)
  2496.                             RealDepth = MaxDepth;
  2497.  
  2498.                         if(RealDepth < ScreenDepth && ScreenDepth <= MaxDepth)
  2499.                             RealDepth = ScreenDepth;
  2500.  
  2501.                         if(RealDepth < ScreenDepth)
  2502.                         {
  2503.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2504.  
  2505.                             ConfigChanged = TRUE;
  2506.                             RethinkPens = TRUE;
  2507.  
  2508.                             break;
  2509.                         }
  2510.                     }
  2511.                     else
  2512.                     {
  2513.                         if(Config->ScreenConfig->Blinking && MaxDepth > 4)
  2514.                             ScreenDepth = 5;
  2515.                         else
  2516.                             ScreenDepth = 4;
  2517.  
  2518.                         if(ScreenDepth > MaxDepth)
  2519.                         {
  2520.                             Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2521.  
  2522.                             ConfigChanged = TRUE;
  2523.                             RethinkPens = TRUE;
  2524.  
  2525.                             break;
  2526.                         }
  2527.  
  2528.                         RealDepth = ScreenDepth;
  2529.                     }
  2530.  
  2531.                     TagArray[Count++] = SA_Pens;
  2532.                     TagArray[Count++] = (LONG)PenArray;
  2533.  
  2534.                     TagArray[Count++] = SA_BlockPen;
  2535.                     TagArray[Count++] = PenArray[SHADOWPEN];
  2536.  
  2537.                     TagArray[Count++] = SA_DetailPen;
  2538.                     TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2539.  
  2540.                     break;
  2541.  
  2542.                 case COLOUR_MONO:
  2543.  
  2544.                     if(Config->ScreenConfig->Depth)
  2545.                         RealDepth = Config->ScreenConfig->Depth;
  2546.                     else
  2547.                         RealDepth = 1;
  2548.  
  2549.                     if(RealDepth > MaxDepth)
  2550.                         RealDepth = MaxDepth;
  2551.  
  2552.                     break;
  2553.  
  2554.                 case COLOUR_AMIGA:
  2555.  
  2556.                     if(Config->ScreenConfig->Depth)
  2557.                         RealDepth = Config->ScreenConfig->Depth;
  2558.                     else
  2559.                         RealDepth = 2;
  2560.  
  2561.                     if(RealDepth > MaxDepth)
  2562.                         RealDepth = MaxDepth;
  2563.  
  2564.                     TagArray[Count++] = SA_Pens;
  2565.                     TagArray[Count++] = (LONG)PenArray;
  2566.  
  2567.                     TagArray[Count++] = SA_BlockPen;
  2568.                     TagArray[Count++] = PenArray[SHADOWPEN];
  2569.  
  2570.                     TagArray[Count++] = SA_DetailPen;
  2571.                     TagArray[Count++] = PenArray[BACKGROUNDPEN];
  2572.  
  2573.                     break;
  2574.             }
  2575.         }
  2576.         while(RethinkPens);
  2577.  
  2578.             /* Add the depth value. */
  2579.  
  2580.         TagArray[Count++] = SA_Depth;
  2581.         TagArray[Count++] = RealDepth;
  2582.  
  2583.             /* Terminate the tag array. */
  2584.  
  2585.         TagArray[Count] = TAG_END;
  2586.  
  2587.             /* Set the plane mask. */
  2588.  
  2589.         DepthMask = (1L << RealDepth) - 1;
  2590.  
  2591.         LimitedSPrintf(sizeof(ScreenTitle),ScreenTitle,LocaleString(MSG_TERMINIT_SCREENTITLE_TXT),TermName,Machine,TermDate,TermIDString);
  2592.  
  2593.         StatusSizeSetup(NULL,&StatusWidth,&StatusHeight);
  2594.  
  2595.         if(StatusHeight && StatusWidth > ScreenWidth)
  2596.             ScreenWidth = StatusWidth;
  2597.  
  2598.         do
  2599.         {
  2600.             if(Screen = (struct Screen *)OpenScreenTags(NULL,
  2601.                 ScreenWidth  ? SA_Width  : TAG_IGNORE,    ScreenWidth,
  2602.                 ScreenHeight ? SA_Height : TAG_IGNORE,    ScreenHeight,
  2603.  
  2604.                 SA_Title,        ScreenTitle,
  2605.                 SA_Left,        DisplayClip.MinX,
  2606.                 SA_DClip,        &DisplayClip,
  2607.                 SA_DisplayID,    Config->ScreenConfig->DisplayMode,
  2608.                 SA_Font,        &UserFont,
  2609.                 SA_Behind,        !Activate,
  2610.                 SA_AutoScroll,    TRUE,
  2611.                 SA_ShowTitle,    Config->ScreenConfig->TitleBar,
  2612.                 SA_PubName,        TermIDString,
  2613.                 SA_ErrorCode,    &ErrorCode,
  2614.                 SA_Interleaved,    Config->ScreenConfig->FasterLayout && Kick30 && RealDepth > 1,
  2615.                 SA_BackFill,    &BackfillHook,
  2616.             TAG_MORE,TagArray))
  2617.             {
  2618.                 if(RealDepth > 1 && !Config->ScreenConfig->FasterLayout)
  2619.                 {
  2620.                     UseMasking = TRUE;
  2621.  
  2622.                     if(Kick30)
  2623.                     {
  2624.                         if(GetBitMapAttr(Screen->RastPort.BitMap,BMA_FLAGS) & BMF_INTERLEAVED)
  2625.                             UseMasking = FALSE;
  2626.                     }
  2627.                 }
  2628.                 else
  2629.                     UseMasking = FALSE;
  2630.             }
  2631.  
  2632.                 /* So we've got an error. */
  2633.  
  2634.             if(!Screen)
  2635.             {
  2636.                 if(OpenFailed)
  2637.                     return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  2638.                 else
  2639.                 {
  2640.                     struct Screen *PubScreen;
  2641.  
  2642.                     switch(ErrorCode)
  2643.                     {
  2644.                             /* Can't open screen with these display
  2645.                              * modes.
  2646.                              */
  2647.  
  2648.                         case OSERR_NOMONITOR:
  2649.                         case OSERR_NOCHIPS:
  2650.                         case OSERR_UNKNOWNMODE:
  2651.                         case OSERR_NOTAVAILABLE:
  2652.  
  2653.                             if(PubScreen = LockPubScreen(NULL))
  2654.                             {
  2655.                                 struct DimensionInfo DimensionInfo;
  2656.  
  2657.                                 Config->ScreenConfig->DisplayMode = GetVPModeID(&PubScreen->ViewPort);
  2658.  
  2659.                                 if(GetDisplayInfoData(NULL,(APTR)&DimensionInfo,sizeof(struct DimensionInfo),DTAG_DIMS,Config->ScreenConfig->DisplayMode))
  2660.                                 {
  2661.                                     LONG Width,Height;
  2662.  
  2663.                                     Width    = DimensionInfo.TxtOScan.MaxX - DimensionInfo.TxtOScan.MinX + 1;
  2664.                                     Height    = DimensionInfo.TxtOScan.MaxY - DimensionInfo.TxtOScan.MinY + 1;
  2665.  
  2666.                                     if(Width != Config->ScreenConfig->DisplayWidth && Config->ScreenConfig->DisplayWidth)
  2667.                                         Config->ScreenConfig->DisplayWidth = Width;
  2668.  
  2669.                                     if(Height != Config->ScreenConfig->DisplayHeight && Config->ScreenConfig->DisplayHeight)
  2670.                                         Config->ScreenConfig->DisplayHeight = Height;
  2671.                                 }
  2672.  
  2673.                                 UnlockPubScreen(NULL,PubScreen);
  2674.                             }
  2675.                             else
  2676.                                 Config->ScreenConfig->DisplayMode = DEFAULT_MONITOR_ID | HIRESLACE_KEY;
  2677.  
  2678.                             OpenFailed = TRUE;
  2679.  
  2680.                             break;
  2681.  
  2682.                         case OSERR_PUBNOTUNIQUE:
  2683.  
  2684.                             return(LocaleString(MSG_TERMINIT_SCREEN_ID_ALREADY_IN_USE_TXT));
  2685.  
  2686.                             /* Some different error, probably out of
  2687.                              * memory.
  2688.                              */
  2689.  
  2690.                         default:
  2691.  
  2692.                             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_SCREEN_TXT));
  2693.                     }
  2694.                 }
  2695.             }
  2696.         }
  2697.         while(Screen == NULL);
  2698.  
  2699.         if(!(DrawInfo = GetScreenDrawInfo(Screen)))
  2700.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_SCREEN_DRAWINFO_TXT));
  2701.         else
  2702.             Pens = DrawInfo->dri_Pens;
  2703.  
  2704.         CreateMenuGlyphs(Screen,DrawInfo,&AmigaGlyph,&CheckGlyph);
  2705.  
  2706.         VPort = &Screen->ViewPort;
  2707.  
  2708.         ScreenWidth        = Screen->Width;
  2709.         ScreenHeight    = Screen->Height;
  2710.  
  2711.         StatusSizeSetup(Screen,&StatusWidth,&StatusHeight);
  2712.  
  2713.             /* Obtain visual info (whatever that may be). */
  2714.  
  2715.         if(!(VisualInfo = GetVisualInfo(Screen,TAG_DONE)))
  2716.         {
  2717.                 /* Delete the DrawInfo now, or it won't
  2718.                  * get freed during shutdown.
  2719.                  */
  2720.  
  2721.             FreeScreenDrawInfo(Screen,DrawInfo);
  2722.  
  2723.             DrawInfo = NULL;
  2724.  
  2725.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OBTAIN_VISUAL_INFO_TXT));
  2726.         }
  2727.  
  2728.         if(Config->ScreenConfig->TitleBar)
  2729.         {
  2730.             Top = Screen->BarHeight + 1;
  2731.  
  2732.             Height = Screen->Height - (Screen->BarHeight + 1);
  2733.         }
  2734.         else
  2735.         {
  2736.             Top = 0;
  2737.  
  2738.             Height = Screen->Height;
  2739.         }
  2740.  
  2741.             /* Open the main window. */
  2742.  
  2743.         if(!(Window = OpenWindowTags(NULL,
  2744.             WA_Top,                Top,
  2745.             WA_Left,            0,
  2746.             WA_Width,            Screen->Width,
  2747.             WA_Height,            Height,
  2748.             WA_Backdrop,        TRUE,
  2749.             WA_Borderless,        TRUE,
  2750.             WA_CustomScreen,    Screen,
  2751.             WA_NewLookMenus,    TRUE,
  2752.             WA_RMBTrap,            TRUE,
  2753.             WA_IDCMP,            DEFAULT_IDCMP & ~IDCMP_CLOSEWINDOW,
  2754.             WA_MenuHelp,        TRUE,
  2755.             WA_NoCareRefresh,    TRUE,
  2756.             WA_Activate,        Activate,
  2757.  
  2758.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  2759.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  2760.         TAG_DONE)))
  2761.         {
  2762.                 /* Delete the DrawInfo now, or it won't
  2763.                  * get freed during shutdown.
  2764.                  */
  2765.  
  2766.             FreeScreenDrawInfo(Screen,DrawInfo);
  2767.  
  2768.             DrawInfo = NULL;
  2769.  
  2770.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_WINDOW_TXT));
  2771.         }
  2772.  
  2773.         if(StatusHeight)
  2774.         {
  2775.             StatusDisplayHeight = StatusHeight;
  2776.  
  2777.             CopyMem(Window->RPort,StatusRPort = &StatusRastPort,sizeof(struct RastPort));
  2778.         }
  2779.         else
  2780.             StatusRPort = NULL;
  2781.     }
  2782.  
  2783.         /* Now check if a nonzero background colour should be used. */
  2784.  
  2785.     if(Pens[BACKGROUNDPEN])
  2786.         BackfillTag = WA_BackFill;
  2787.     else
  2788.         BackfillTag = TAG_IGNORE;
  2789.  
  2790.         /* Fill the `default' colour with current values. */
  2791.  
  2792.     if(!Config->ScreenConfig->UseWorkbench && Initializing && !SharedScreen)
  2793.     {
  2794.         if(Kick30)
  2795.         {
  2796.             ULONG RGB[3 * 16];
  2797.  
  2798.             GetRGB32(VPort->ColorMap,0,16,RGB);
  2799.  
  2800.             ANSIColourTable        = CreateColourTable(8,    ANSIColours,    NULL);
  2801.             EGAColourTable        = CreateColourTable(16,    EGAColours,    NULL);
  2802.             DefaultColourTable    = CreateColourTable(16,    NULL,        RGB);
  2803.             MonoColourTable        = CreateColourTable(2,    AtomicColours,    NULL);
  2804.         }
  2805.  
  2806.         for(i = 0 ; i < 16 ; i++)
  2807.             DefaultColours[i] = GetRGB4(VPort->ColorMap,i);
  2808.  
  2809.         Initializing = FALSE;
  2810.     }
  2811.  
  2812.         /* Load the approriate colours. */
  2813.  
  2814.     if(LoadColours)
  2815.     {
  2816.         Default2CurrentPalette(Config);
  2817.  
  2818.         LoadColours = FALSE;
  2819.     }
  2820.  
  2821.         /* Reset the current colours and the blinking equivalents. */
  2822.  
  2823.     PaletteSetup(Config);
  2824.  
  2825.     if(!Config->ScreenConfig->UseWorkbench && !SharedScreen)
  2826.         LoadColourTable(VPort,NormalColourTable,NormalColours,PaletteSize);
  2827.  
  2828.         /* Get the vanilla rendering pens. */
  2829.  
  2830.     RenderPens[0] = Pens[BACKGROUNDPEN];
  2831.     RenderPens[1] = Pens[TEXTPEN];
  2832.     RenderPens[2] = Pens[SHINEPEN];
  2833.     RenderPens[3] = Pens[FILLPEN];
  2834.  
  2835.         /* Are we to use the Workbench screen for text output? */
  2836.  
  2837.     if(Config->ScreenConfig->UseWorkbench || (SharedScreen && Kick30))
  2838.     {
  2839.         BOOL UseDefaults;
  2840.  
  2841.         if(Kick30 && (Config->ScreenConfig->ColourMode == COLOUR_EIGHT || Config->ScreenConfig->ColourMode == COLOUR_SIXTEEN))
  2842.         {
  2843.             LONG NumPens;
  2844.             ULONG R,G,B;
  2845.             BOOL GotAll;
  2846.  
  2847.             UseDefaults = FALSE;
  2848.  
  2849.             do
  2850.             {
  2851.                 if(Config->ScreenConfig->ColourMode == COLOUR_EIGHT)
  2852.                     NumPens = 8;
  2853.                 else
  2854.                     NumPens = 16;
  2855.  
  2856.                 for(i = 0 ; i < 32 ; i++)
  2857.                     MappedPens[1][i] = FALSE;
  2858.  
  2859.                 GotAll = TRUE;
  2860.  
  2861.                     /* Allocate the text rendering pens, note that
  2862.                      * we will use the currently installed palette
  2863.                      * to obtain those pens which match them best.
  2864.                      * The user will be unable to change these
  2865.                      * colours.
  2866.                      */
  2867.  
  2868.                 if(NormalColourTable)
  2869.                 {
  2870.                     for(i = 0 ; i < NumPens ; i++)
  2871.                     {
  2872.                             /* Try to obtain a matching pen. */
  2873.  
  2874.                         if((MappedPens[0][i] = ObtainBestPen(VPort->ColorMap,NormalColourTable->Entry[i].Red,NormalColourTable->Entry[i].Green,NormalColourTable->Entry[i].Blue,
  2875.                             OBP_FailIfBad,TRUE,
  2876.                         TAG_DONE)) == -1)
  2877.                         {
  2878.                             MappedPens[1][i] = FALSE;
  2879.  
  2880.                             GotAll = FALSE;
  2881.  
  2882.                             break;
  2883.                         }
  2884.                         else
  2885.                             MappedPens[1][i] = TRUE;
  2886.                     }
  2887.                 }
  2888.                 else
  2889.                 {
  2890.                     for(i = 0 ; i < NumPens ; i++)
  2891.                     {
  2892.                             /* Split the 12 bit colour palette entry. */
  2893.  
  2894.                         R = (NormalColours[i] >> 8) & 0xF;
  2895.                         G = (NormalColours[i] >> 4) & 0xF;
  2896.                         B =  NormalColours[i]       & 0xF;
  2897.  
  2898.                             /* Try to obtain a matching pen. */
  2899.  
  2900.                         if((MappedPens[0][i] = ObtainBestPen(VPort->ColorMap,SPREAD((R << 4) | R),SPREAD((G << 4) | G),SPREAD((B << 4) | B),
  2901.                             OBP_FailIfBad,TRUE,
  2902.                         TAG_DONE)) == -1)
  2903.                         {
  2904.                             MappedPens[1][i] = FALSE;
  2905.  
  2906.                             GotAll = FALSE;
  2907.  
  2908.                             break;
  2909.                         }
  2910.                         else
  2911.                             MappedPens[1][i] = TRUE;
  2912.                     }
  2913.                 }
  2914.  
  2915.                     /* Did we get what we wanted? */
  2916.  
  2917.                 if(!GotAll)
  2918.                 {
  2919.                         /* Release all the pens we succeeded
  2920.                          * in allocating.
  2921.                          */
  2922.  
  2923.                     for(i = 0 ; i < NumPens ; i++)
  2924.                     {
  2925.                         if(MappedPens[1][i])
  2926.                             ReleasePen(VPort->ColorMap,MappedPens[0][i]);
  2927.                     }
  2928.  
  2929.                         /* Check if we could step back */
  2930.  
  2931.                     if(Config->ScreenConfig->ColourMode = COLOUR_SIXTEEN)
  2932.                         Config->ScreenConfig->ColourMode = COLOUR_EIGHT;
  2933.                     else
  2934.                     {
  2935.                         Config->ScreenConfig->ColourMode = COLOUR_AMIGA;
  2936.  
  2937.                         UseDefaults = TRUE;
  2938.                     }
  2939.                 }
  2940.                 else
  2941.                     AllocatedPens = TRUE;
  2942.             }
  2943.             while(!AllocatedPens && !UseDefaults);
  2944.         }
  2945.         else
  2946.             UseDefaults = TRUE;
  2947.  
  2948.             /* Use the default rendering pens? */
  2949.  
  2950.         if(UseDefaults)
  2951.         {
  2952.             if(Config->ScreenConfig->ColourMode == COLOUR_AMIGA)
  2953.             {
  2954.                 for(i = 0 ; i < 4 ; i++)
  2955.                 {
  2956.                     MappedPens[0][i] = RenderPens[i];
  2957.                     MappedPens[1][i] = FALSE;
  2958.                 }
  2959.  
  2960.                     /* Set the remaining pens to defaults. */
  2961.  
  2962.                 for(i = 4 ; i < 16 ; i++)
  2963.                 {
  2964.                     MappedPens[0][i] = RenderPens[1];
  2965.                     MappedPens[1][i] = FALSE;
  2966.                 }
  2967.             }
  2968.             else
  2969.             {
  2970.                 for(i = 0 ; i < 16 ; i++)
  2971.                 {
  2972.                     MappedPens[0][i] = RenderPens[i & 1];
  2973.                     MappedPens[1][i] = FALSE;
  2974.                 }
  2975.             }
  2976.         }
  2977.  
  2978.         for(i = 0 ; i < 16 ; i++)
  2979.         {
  2980.             MappedPens[0][i + 16] = MappedPens[0][i];
  2981.             MappedPens[1][i + 16] = FALSE;
  2982.         }
  2983.     }
  2984.     else
  2985.     {
  2986.             /* Reset the colour translation table. */
  2987.  
  2988.         for(i = 0 ; i < 32 ; i++)
  2989.         {
  2990.             MappedPens[0][i] = i;
  2991.             MappedPens[1][i] = FALSE;
  2992.         }
  2993.     }
  2994.  
  2995.         /* Determine default text rendering colour. */
  2996.  
  2997.     switch(Config->ScreenConfig->ColourMode)
  2998.     {
  2999.         case COLOUR_SIXTEEN:
  3000.  
  3001.             SafeTextPen = MappedPens[0][7];
  3002.             break;
  3003.  
  3004.         case COLOUR_EIGHT:
  3005.  
  3006.             SafeTextPen = MappedPens[0][7];
  3007.             break;
  3008.  
  3009.         case COLOUR_AMIGA:
  3010.         case COLOUR_MONO:
  3011.  
  3012.             SafeTextPen = MappedPens[0][1];
  3013.             break;
  3014.     }
  3015.  
  3016.         /* Take care of pen and attribute mapping. */
  3017.  
  3018.     if(Config->EmulationConfig->UseStandardPens)
  3019.     {
  3020.         for(i = 0 ; i < 16 ; i++)
  3021.         {
  3022.             PenTable[i]        = i;
  3023.             TextAttributeTable[i]    = i;
  3024.         }
  3025.     }
  3026.     else
  3027.     {
  3028.         UBYTE Value;
  3029.  
  3030.         CopyMem(Config->EmulationConfig->Pens,PenTable,sizeof(Config->EmulationConfig->Pens));
  3031.  
  3032.         for(i = 0 ; i < 16 ; i++)
  3033.         {
  3034.             Value = 0;
  3035.  
  3036.             if(i & ATTR_UNDERLINE)
  3037.             {
  3038.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_UNDERLINE])
  3039.                 {
  3040.                     case TEXTATTR_UNDERLINE:
  3041.  
  3042.                         Value |= ATTR_UNDERLINE;
  3043.                         break;
  3044.  
  3045.                     case TEXTATTR_HIGHLIGHT:
  3046.  
  3047.                         Value |= ATTR_HIGHLIGHT;
  3048.                         break;
  3049.  
  3050.                     case TEXTATTR_BLINK:
  3051.  
  3052.                         Value |= ATTR_BLINK;
  3053.                         break;
  3054.  
  3055.                     case TEXTATTR_INVERSE:
  3056.  
  3057.                         Value |= ATTR_INVERSE;
  3058.                         break;
  3059.                 }
  3060.             }
  3061.  
  3062.             if(i & ATTR_HIGHLIGHT)
  3063.             {
  3064.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_HIGHLIGHT])
  3065.                 {
  3066.                     case TEXTATTR_UNDERLINE:
  3067.  
  3068.                         Value |= ATTR_UNDERLINE;
  3069.                         break;
  3070.  
  3071.                     case TEXTATTR_HIGHLIGHT:
  3072.  
  3073.                         Value |= ATTR_HIGHLIGHT;
  3074.                         break;
  3075.  
  3076.                     case TEXTATTR_BLINK:
  3077.  
  3078.                         Value |= ATTR_BLINK;
  3079.                         break;
  3080.  
  3081.                     case TEXTATTR_INVERSE:
  3082.  
  3083.                         Value |= ATTR_INVERSE;
  3084.                         break;
  3085.                 }
  3086.             }
  3087.  
  3088.             if(i & ATTR_BLINK)
  3089.             {
  3090.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_BLINK])
  3091.                 {
  3092.                     case TEXTATTR_UNDERLINE:
  3093.  
  3094.                         Value |= ATTR_UNDERLINE;
  3095.                         break;
  3096.  
  3097.                     case TEXTATTR_HIGHLIGHT:
  3098.  
  3099.                         Value |= ATTR_HIGHLIGHT;
  3100.                         break;
  3101.  
  3102.                     case TEXTATTR_BLINK:
  3103.  
  3104.                         Value |= ATTR_BLINK;
  3105.                         break;
  3106.  
  3107.                     case TEXTATTR_INVERSE:
  3108.  
  3109.                         Value |= ATTR_INVERSE;
  3110.                         break;
  3111.                 }
  3112.             }
  3113.  
  3114.             if(i & ATTR_INVERSE)
  3115.             {
  3116.                 switch(Config->EmulationConfig->Attributes[TEXTATTR_INVERSE])
  3117.                 {
  3118.                     case TEXTATTR_UNDERLINE:
  3119.  
  3120.                         Value |= ATTR_UNDERLINE;
  3121.                         break;
  3122.  
  3123.                     case TEXTATTR_HIGHLIGHT:
  3124.  
  3125.                         Value |= ATTR_HIGHLIGHT;
  3126.                         break;
  3127.  
  3128.                     case TEXTATTR_BLINK:
  3129.  
  3130.                         Value |= ATTR_BLINK;
  3131.                         break;
  3132.  
  3133.                     case TEXTATTR_INVERSE:
  3134.  
  3135.                         Value |= ATTR_INVERSE;
  3136.                         break;
  3137.                 }
  3138.             }
  3139.  
  3140.             TextAttributeTable[i] = Value;
  3141.         }
  3142.     }
  3143.  
  3144.         /* Determine window inner dimensions and top/left edge offsets. */
  3145.  
  3146.     UpdateTerminalLimits();
  3147.  
  3148.         /* Set up scaling data (bitmaps & rastports). */
  3149.  
  3150.     if(!CreateScale(Window))
  3151.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_FONT_SCALING_INFO_TXT));
  3152.  
  3153.     if(!CreateOffsetTables())
  3154.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_OFFSET_TABLES_TXT));
  3155.  
  3156.     TabStopMax = Window->WScreen->Width / TextFontWidth;
  3157.  
  3158.         /* Allocate the tab stop line. */
  3159.  
  3160.     if(!(TabStops = (BYTE *)AllocVecPooled(TabStopMax,MEMF_ANY | MEMF_CLEAR)))
  3161.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3162.  
  3163.         /* Reset the Window job handler to the default routine. */
  3164.  
  3165.     WindowJob = SingleWindowJob;
  3166.  
  3167.         /* Push it on the window stack (should become bottommost
  3168.          * entry).
  3169.          */
  3170.  
  3171.     PushWindow(Window);
  3172.  
  3173.     if(TermPort)
  3174.         TermPort->TopWindow = Window;
  3175.  
  3176.     if(Config->ScreenConfig->StatusLine != STATUSLINE_DISABLED && Config->ScreenConfig->SplitStatus)
  3177.     {
  3178.         if(!(StatusWindow = OpenWindowTags(NULL,
  3179.             WA_Top,                Window->TopEdge + Window->Height,
  3180.             WA_Left,            Window->LeftEdge,
  3181.             WA_InnerWidth,        StatusWidth,
  3182.             WA_InnerHeight,        StatusHeight,
  3183.             WA_DragBar,            TRUE,
  3184.             WA_DepthGadget,        TRUE,
  3185.             WA_NewLookMenus,    TRUE,
  3186.             WA_Title,            ScreenTitle,
  3187.             OpenWindowTag,        Window->WScreen,
  3188.             WA_RMBTrap,            TRUE,
  3189.             WA_CloseGadget,        TRUE,
  3190.             WA_MenuHelp,        TRUE,
  3191.             WA_NoCareRefresh,    TRUE,
  3192.  
  3193.             AmigaGlyph ? WA_AmigaKey  : TAG_IGNORE, AmigaGlyph,
  3194.             CheckGlyph ? WA_Checkmark : TAG_IGNORE, CheckGlyph,
  3195.         TAG_DONE)))
  3196.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_STATUS_WINDOW_TXT));
  3197.     }
  3198.     else
  3199.         StatusWindow = NULL;
  3200.  
  3201.     if(StatusWindow)
  3202.     {
  3203.         StatusWindow->UserPort = Window->UserPort;
  3204.  
  3205.         if(!ModifyIDCMP(StatusWindow,DEFAULT_IDCMP))
  3206.         {
  3207.             StatusWindow->UserPort = NULL;
  3208.  
  3209.             CloseWindow(StatusWindow);
  3210.             StatusWindow = NULL;
  3211.  
  3212.             return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_STATUS_WINDOW_TXT));
  3213.         }
  3214.     }
  3215.  
  3216.     UpdateTerminalLimits();
  3217.  
  3218.     RPort = Window->RPort;
  3219.  
  3220.         /* Default console setup. */
  3221.  
  3222.     CursorX = 0;
  3223.     CursorY    = 0;
  3224.  
  3225.     SetDrMd(RPort,JAM2);
  3226.  
  3227.         /* Set the font. */
  3228.  
  3229.     SetFont(RPort,CurrentFont);
  3230.  
  3231.         /* Redirect AmigaDOS requesters. */
  3232.  
  3233.     ChangeWindowPtr(&OldWindowPtr,(APTR)Window);
  3234.  
  3235.         /* Create the character raster. */
  3236.  
  3237.     if(!CreateRaster())
  3238.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_SCREEN_RASTER_TXT));
  3239.  
  3240.     ConOutputUpdate();
  3241.  
  3242.     ConFontScaleUpdate();
  3243.  
  3244.         /* Set up the scrolling info. */
  3245.  
  3246.     ScrollLineCount = Window->WScreen->Height / TextFontHeight;
  3247.  
  3248.     if(!(ScrollLines = (struct ScrollLineInfo *)AllocVecPooled(sizeof(struct ScrollLineInfo) * ScrollLineCount,MEMF_ANY|MEMF_CLEAR)))
  3249.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SCROLLING_SUPPORT_INFO_TXT));
  3250.  
  3251.         /* Create the menu strip. */
  3252.  
  3253.     if(!AttachMenu(NULL))
  3254.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_MENUS_TXT));
  3255.  
  3256.         /* Disable the `Execute ARexx Command' menu item if
  3257.          * the rexx server is not available.
  3258.          */
  3259.  
  3260.     if(!RexxSysBase)
  3261.         OffItem(MEN_EXECUTE_REXX_COMMAND);
  3262.  
  3263.     if(Recording)
  3264.     {
  3265.         OnItem(MEN_RECORD_LINE);
  3266.  
  3267.         CheckItem(MEN_RECORD,TRUE);
  3268.         CheckItem(MEN_RECORD_LINE,RecordingLine);
  3269.     }
  3270.     else
  3271.     {
  3272.         OffItem(MEN_RECORD_LINE);
  3273.  
  3274.         CheckItem(MEN_RECORD,FALSE);
  3275.         CheckItem(MEN_RECORD_LINE,FALSE);
  3276.     }
  3277.  
  3278.     CheckItem(MEN_DISABLE_TRAPS,!(WatchTraps && !IsListEmpty((struct List *)GenericListTable[GLIST_TRAP])));
  3279.  
  3280.     if(StatusWindow)
  3281.     {
  3282.         SetMenuStrip(StatusWindow,Menu);
  3283.  
  3284.         StatusWindow->Flags &= ~WFLG_RMBTRAP;
  3285.  
  3286.         SetDrMd(StatusWindow->RPort,JAM2);
  3287.     }
  3288.  
  3289.         /* Add a tick if file capture is active. */
  3290.  
  3291.     if(RawCapture)
  3292.     {
  3293.         if(FileCapture)
  3294.             CheckItem(MEN_CAPTURE_TO_RAW_FILE,TRUE);
  3295.         else
  3296.             CheckItem(MEN_CAPTURE_TO_RAW_FILE,FALSE);
  3297.  
  3298.         CheckItem(MEN_CAPTURE_TO_FILE,FALSE);
  3299.     }
  3300.     else
  3301.     {
  3302.         if(FileCapture)
  3303.             CheckItem(MEN_CAPTURE_TO_FILE,TRUE);
  3304.         else
  3305.             CheckItem(MEN_CAPTURE_TO_FILE,FALSE);
  3306.     }
  3307.  
  3308.         /* Add a tick if printer capture is active. */
  3309.  
  3310.     CheckItem(MEN_CAPTURE_TO_PRINTER,PrinterCapture != NULL);
  3311.  
  3312.         /* Add a tick if the buffer is frozen. */
  3313.  
  3314.     CheckItem(MEN_FREEZE_BUFFER,BufferFrozen);
  3315.  
  3316.         /* Disable the dialing functions if online. */
  3317.  
  3318.     if(Online)
  3319.         SetDialMenu(FALSE);
  3320.     else
  3321.         SetDialMenu(TRUE);
  3322.  
  3323.         /* Take care of the chat line. */
  3324.  
  3325.     if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0])
  3326.         OffItem(MEN_CHAT_LINE);
  3327.     else
  3328.         CheckItem(MEN_CHAT_LINE,ChatMode);
  3329.  
  3330.         /* Update the clipboard menus. */
  3331.  
  3332.     SetClipMenu(FALSE);
  3333.  
  3334.     SetTransferMenu(TRUE);
  3335.  
  3336.         /* Disable the `Print Screen' and `Save ASCII' functions
  3337.          * if raster is not enabled.
  3338.          */
  3339.  
  3340.     SetRasterMenu(RasterEnabled);
  3341.  
  3342.         /* Take care of the remaining windows */
  3343.  
  3344.     if(MatrixWindow)
  3345.         CheckItem(MEN_MATRIX_WINDOW,TRUE);
  3346.  
  3347.     if(InfoWindow)
  3348.         CheckItem(MEN_STATUS_WINDOW,TRUE);
  3349.  
  3350.     if(PacketWindow)
  3351.         CheckItem(MEN_PACKET_WINDOW,TRUE);
  3352.  
  3353.     if(ChatMode)
  3354.         CheckItem(MEN_CHAT_LINE,TRUE);
  3355.  
  3356.     if(FastWindow)
  3357.         CheckItem(MEN_FAST_MACROS_WINDOW,TRUE);
  3358.  
  3359.         /* Enable the menu. */
  3360.  
  3361.     Window->Flags &= ~WFLG_RMBTRAP;
  3362.  
  3363.     strcpy(EmulationName,LocaleString(MSG_TERMXEM_NO_EMULATION_TXT));
  3364.  
  3365.         /* Create the status server. */
  3366.  
  3367.     Forbid();
  3368.  
  3369.     if(StatusProcess = CreateNewProcTags(
  3370.         NP_Entry,        StatusServer,
  3371.         NP_Name,        "term Status Process",
  3372.         NP_WindowPtr,    -1,
  3373.         NP_Priority,    5,
  3374.     TAG_DONE))
  3375.         WaitForHandshake();
  3376.  
  3377.     Permit();
  3378.  
  3379.     if(!StatusProcess)
  3380.         return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_STATUS_TASK_TXT));
  3381.  
  3382.         /* Obtain the default public screen name just in case
  3383.          * we'll need it later.
  3384.          */
  3385.  
  3386.     GetDefaultPubScreen(DefaultPubScreenName);
  3387.  
  3388.         /* Set up the window size. */
  3389.  
  3390.     ScreenSizeStuff();
  3391.  
  3392.         /* Select the default console data processing routine. */
  3393.  
  3394.     ConProcessUpdate();
  3395.  
  3396.         /* Handle the remaining terminal setup. */
  3397.  
  3398.     if(Config->TerminalConfig->EmulationMode == EMULATION_EXTERNAL && Config->TerminalConfig->EmulationFileName[0])
  3399.     {
  3400.         if(!OpenEmulator(Config->TerminalConfig->EmulationFileName))
  3401.         {
  3402.             Config->TerminalConfig->EmulationMode = EMULATION_ANSIVT100;
  3403.  
  3404.             ResetDisplay = TRUE;
  3405.             ActivateJob(MainJobQueue,ResetDisplayJob);
  3406.  
  3407.             RasterEnabled = TRUE;
  3408.  
  3409.             ShowRequest(Window,LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_EMULATION_LIBRARY_TXT),Config->TerminalConfig->EmulationFileName);
  3410.         }
  3411.         else
  3412.         {
  3413.             if(RasterEnabled)
  3414.                 RasterEnabled = FALSE;
  3415.  
  3416.             SetRasterMenu(RasterEnabled);
  3417.         }
  3418.     }
  3419.  
  3420.         /* Choose the right console data processing routine. */
  3421.  
  3422.     ConProcessUpdate();
  3423.  
  3424.         /* Reset terminal emulation. */
  3425.  
  3426.     if(Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL)
  3427.     {
  3428.         ClearCursor();
  3429.  
  3430.         ForegroundPen = GetPenIndex(SafeTextPen);
  3431.         BackgroundPen = 0;
  3432.  
  3433.         UpdatePens();
  3434.  
  3435.         Reset();
  3436.  
  3437.         DrawCursor();
  3438.     }
  3439.     else
  3440.     {
  3441.         if(XEmulatorBase)
  3442.             XEmulatorResetConsole(XEM_IO);
  3443.     }
  3444.  
  3445.         /* Restart the fast! macro panel. */
  3446.  
  3447.     if(HadFastMacros || Config->MiscConfig->OpenFastMacroPanel)
  3448.         OpenFastWindow();
  3449.  
  3450.     if(Config->TerminalConfig->UseTerminalTask && Config->TerminalConfig->EmulationMode != EMULATION_EXTERNAL)
  3451.         CreateEmulationProcess();
  3452.     else
  3453.         DeleteEmulationProcess();
  3454.  
  3455.     TTYResize();
  3456.  
  3457.     UpdateJob(MainJobQueue,WindowJob,SIG_WINDOW);
  3458.  
  3459.     return(NULL);
  3460. }
  3461.  
  3462.     /* CloseAll():
  3463.      *
  3464.      *    Free all resources and leave the program.
  3465.      */
  3466.  
  3467. VOID
  3468. CloseAll()
  3469. {
  3470.     RemoveRendezvousSemaphore();
  3471.  
  3472.     DispatchRexxDialMsgList(FALSE);
  3473.  
  3474.     DeleteRecord();
  3475.  
  3476.     DeleteQueueProcess();
  3477.  
  3478.     SoundExit();
  3479.  
  3480.     SZ_SizeCleanup();
  3481.  
  3482.     if(IntuitionBase && Window)
  3483.         BlockWindows();
  3484.  
  3485.     if(RexxPort)
  3486.         RemPort(RexxPort);
  3487.  
  3488.     DeleteChatGadget();
  3489.  
  3490.     if(TermRexxPort)
  3491.     {
  3492.         if(RexxSysBase)
  3493.         {
  3494.             struct Message *Msg;
  3495.  
  3496.             while(Msg = GetMsg(TermRexxPort))
  3497.                 ReplyMsg(Msg);
  3498.         }
  3499.  
  3500.         DeleteMsgPort(TermRexxPort);
  3501.     }
  3502.  
  3503.     ShakeHands((struct Task *)RexxProcess,SIG_KILL);
  3504.  
  3505.     CloseLibrary((struct Library *)RexxSysBase);
  3506.  
  3507.     if(XprIO && XProtocolBase)
  3508.         XProtocolCleanup(XprIO);
  3509.  
  3510.     CloseLibrary(XProtocolBase);
  3511.  
  3512.     FreeVec(XprIO);
  3513.  
  3514.     TerminateBuffer();
  3515.  
  3516.     DeleteSpeech();
  3517.  
  3518.     Forbid();
  3519.  
  3520.     BufferClosed = TRUE;
  3521.  
  3522.     DeleteBuffer();
  3523.  
  3524.     Permit();
  3525.  
  3526.     if(FileCapture)
  3527.     {
  3528.         BufferClose(FileCapture);
  3529.  
  3530.         if(!GetFileSize(CaptureName))
  3531.             DeleteFile(CaptureName);
  3532.         else
  3533.         {
  3534.             AddProtection(CaptureName,FIBF_EXECUTE);
  3535.  
  3536.             if(Config->MiscConfig->CreateIcons)
  3537.                 AddIcon(CaptureName,FILETYPE_TEXT,FALSE);
  3538.         }
  3539.     }
  3540.  
  3541.     if(PrinterCapture)
  3542.         Close(PrinterCapture);
  3543.  
  3544.         /* Close the external emulator. */
  3545.  
  3546.     CloseEmulator(TRUE);
  3547.  
  3548.     if(IntuitionBase && GfxBase)
  3549.         DeleteDisplay();
  3550.  
  3551.     CaptureParserExit();
  3552.  
  3553.     UnLoadSeg(KeySegment);
  3554.  
  3555.     StopCall(TRUE);
  3556.  
  3557.     DeleteAccountant();
  3558.  
  3559.     FreeSignal(CheckBit);
  3560.  
  3561.     ClearSerial();
  3562.  
  3563.     DeleteSerial();
  3564.  
  3565.     StopTime();
  3566.  
  3567.     if(TimeRequest)
  3568.     {
  3569.         if(TimeRequest->tr_node.io_Device)
  3570.             CloseDevice((struct IORequest *)TimeRequest);
  3571.  
  3572.         DeleteIORequest((struct IORequest *)TimeRequest);
  3573.     }
  3574.  
  3575.     DeleteMsgPort(TimePort);
  3576.  
  3577.     ShutdownCx();
  3578.  
  3579.     if(TermPort)
  3580.     {
  3581.         if(TermID != -1)
  3582.         {
  3583.             ObtainSemaphore(&TermPort->OpenSemaphore);
  3584.  
  3585.             TermPort->OpenCount--;
  3586.  
  3587.             if(TermPort->OpenCount <= 0 && !TermPort->HoldIt)
  3588.             {
  3589.                 RemPort(&TermPort->MsgPort);
  3590.  
  3591.                 ReleaseSemaphore(&TermPort->OpenSemaphore);
  3592.  
  3593.                 FreeVec(TermPort);
  3594.             }
  3595.             else
  3596.                 ReleaseSemaphore(&TermPort->OpenSemaphore);
  3597.         }
  3598.     }
  3599.  
  3600.     FreeSignal(OwnDevBit);
  3601.     CloseLibrary(OwnDevUnitBase);
  3602.  
  3603.     CloseClip();
  3604.  
  3605.     LocaleClose();
  3606.  
  3607.     DeleteMsgQueue(SpecialQueue);
  3608.  
  3609.     if(ConsoleDevice)
  3610.         CloseDevice((struct IORequest *)ConsoleRequest);
  3611.  
  3612.     CloseLibrary(GTLayoutBase);
  3613.  
  3614.     CloseLibrary(IconBase);
  3615.  
  3616.     CloseLibrary(DataTypesBase);
  3617.  
  3618.     CloseLibrary(WorkbenchBase);
  3619.  
  3620.     CloseLibrary(CxBase);
  3621.  
  3622.     CloseLibrary(IFFParseBase);
  3623.  
  3624.     CloseLibrary(AslBase);
  3625.  
  3626.     CloseLibrary(GadToolsBase);
  3627.  
  3628.     CloseLibrary(LayersBase);
  3629.  
  3630.     CloseLibrary((struct Library *)GfxBase);
  3631.  
  3632.     CloseLibrary((struct Library *)IntuitionBase);
  3633.  
  3634.     MemoryCleanup();
  3635. }
  3636.  
  3637.     /* OpenAll():
  3638.      *
  3639.      *    Open all required resources or return an error message
  3640.      *    if anything went wrong.
  3641.      */
  3642.  
  3643. STRPTR
  3644. OpenAll(STRPTR ConfigPath)
  3645. {
  3646.     UBYTE PathBuffer[MAX_FILENAME_LENGTH];
  3647.     STRPTR Result,ConfigFileName;
  3648.     UBYTE LocalBuffer[256];
  3649.     LONG ErrorCode,i;
  3650.     BOOL ZapPhonebook;
  3651.  
  3652.     ConfigFileName = NULL;
  3653.  
  3654.     Kick30 = (SysBase->LibNode.lib_Version >= 39);
  3655.  
  3656.         /* These are here in order to avoid trouble later */
  3657.  
  3658.     ConTransferUpdate();
  3659.     SerWriteUpdate();
  3660.  
  3661.         /* Remember the start of this session. */
  3662.  
  3663.     DateStamp(&SessionStart);
  3664.  
  3665.         /* Set up the hooks in Data.c */
  3666.  
  3667.     InitHook(&LocaleHook,(HOOKFUNC)LocaleHookFunc,NULL);
  3668.     InitHook(&GuideHook,(HOOKFUNC)GuideSetupHook,NULL);
  3669.     InitHook(&BackfillHook,(HOOKFUNC)BackfillRoutine,NULL);
  3670.  
  3671.         /* Set up font DPI tag list. */
  3672.  
  3673.     TagDPI[0].ti_Tag = TAG_DONE;
  3674.  
  3675.         /* Initialize all the semaphores */
  3676.  
  3677.     for(i = 0 ; StartupSemaphoreTable[i] != NULL ; i++)
  3678.         InitSemaphore(StartupSemaphoreTable[i]);
  3679.  
  3680.         /* Initialize all the lists */
  3681.  
  3682.     for(i = 0 ; StartupListTable[i] != NULL ; i++)
  3683.         NewList(StartupListTable[i]);
  3684.  
  3685.     if(!MemorySetup())
  3686.         return("Cannot create memory pool");
  3687.  
  3688.         /* Rendezvous setup. */
  3689.  
  3690.     RendezvousSetup();
  3691.  
  3692.         /* Set up the program launcher. */
  3693.  
  3694.     LaunchSetup();
  3695.  
  3696.         /* Open the translation tables. */
  3697.  
  3698.     LocaleOpen("term.catalog","english",20);
  3699.  
  3700.         /* Fill in the menu configuration. */
  3701.  
  3702.     PrepareLocalizedMenu(&TermMenu,&NumMenuEntries);
  3703.  
  3704.         /* Open intuition.library, any version. */
  3705.  
  3706.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  3707.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_INTUITION_LIBRARY_TXT));
  3708.  
  3709.         /* Check if we should use the old style sliders. */
  3710.  
  3711.     if(GetVar("termoldsliders",PathBuffer,sizeof(PathBuffer),NULL) >= 0)
  3712.         SliderType = SLIDER_KIND;
  3713.     else
  3714.         SliderType = LEVEL_KIND;
  3715.  
  3716.         /* Open some more libraries. */
  3717.  
  3718.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
  3719.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GRAPHICS_LIBRARY_TXT));
  3720.  
  3721.     if(!(LayersBase = OpenLibrary("layers.library",0)))
  3722.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_LAYERS_LIBRARY_TXT));
  3723.  
  3724.         /* Install the correct routines to query
  3725.          * the rendering colours and drawing mode.
  3726.          */
  3727.  
  3728.     if(!Kick30)
  3729.     {
  3730.         ReadAPen = OldGetAPen;
  3731.         ReadBPen = OldGetBPen;
  3732.         ReadDrMd = OldGetDrMd;
  3733.         SetMask = OldSetWrMsk;
  3734.         GetMask = OldGetWrMsk;
  3735.     }
  3736.     else
  3737.     {
  3738.         ReadAPen = NewGetAPen;
  3739.         ReadBPen = NewGetBPen;
  3740.         ReadDrMd = NewGetDrMd;
  3741.         SetMask = NewSetWrMsk;
  3742.         GetMask = NewGetWrMsk;
  3743.     }
  3744.  
  3745.         /* Check if locale.library has already installed the operating system
  3746.          * patches required for localization.
  3747.          */
  3748.  
  3749.     LanguageCheck();
  3750.  
  3751.         /* Open the remaining libraries. */
  3752.  
  3753.     if(!(GadToolsBase = OpenLibrary("gadtools.library",0)))
  3754.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GADTOOLS_LIBRARY_TXT));
  3755.  
  3756.     if(!(AslBase = OpenLibrary("asl.library",0)))
  3757.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_ASL_LIBRARY_TXT));
  3758.  
  3759.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  3760.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_IFFPARSE_LIBRARY_TXT));
  3761.  
  3762.     if(!(CxBase = OpenLibrary("commodities.library",0)))
  3763.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_COMMODITIES_LIBRARY_TXT));
  3764.  
  3765.     if(!(TimePort = (struct MsgPort *)CreateMsgPort()))
  3766.         return(LocaleString(MSG_GLOBAL_FAILED_TO_CREATE_MSGPORT_TXT));
  3767.  
  3768.     if(!(TimeRequest = (struct timerequest *)CreateIORequest(TimePort,sizeof(struct timerequest))))
  3769.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_IOREQUEST_TXT));
  3770.  
  3771.     if(OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest *)TimeRequest,0))
  3772.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_TIMER_DEVICE_TXT));
  3773.  
  3774.     TimerBase = TimeRequest->tr_node.io_Device;
  3775.  
  3776.         /* User interface. */
  3777.  
  3778.     if(!(GTLayoutBase = SafeOpenLibrary("PROGDIR:gtlayout.library",36)))
  3779.     {
  3780.         if(!(GTLayoutBase = SafeOpenLibrary("gtlayout.library",36)))
  3781.         {
  3782.             STATIC UBYTE LocalBuffer[256];
  3783.  
  3784.             STRPTR String;
  3785.             BOOL GotIt;
  3786.             LONG i;
  3787.  
  3788.             String = LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_GTLAYOUT_LIBRARY_TXT);
  3789.  
  3790.             GotIt = FALSE;
  3791.  
  3792.             for(i = 0 ; i < strlen(String) ; i++)
  3793.             {
  3794.                 if(String[i] == '%')
  3795.                 {
  3796.                     GotIt = TRUE;
  3797.                     break;
  3798.                 }
  3799.             }
  3800.  
  3801.             if(GotIt)
  3802.                 LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,String,36);
  3803.             else
  3804.                 LimitedSPrintf(sizeof(LocalBuffer),LocalBuffer,"%s (V%ld)",String,36);
  3805.  
  3806.             return(LocalBuffer);
  3807.         }
  3808.     }
  3809.  
  3810.     if(GetVar("termoldcycle",PathBuffer,sizeof(PathBuffer),NULL) >= 0 || GetVar("RussLeBar",PathBuffer,sizeof(PathBuffer),NULL) >= 0)
  3811.     {
  3812.         LONG Value;
  3813.  
  3814.         CycleType = CYCLE_KIND;
  3815.  
  3816.         if(StrToLong(PathBuffer,&Value) > 0)
  3817.         {
  3818.             if(!Value)
  3819.                 CycleType = POPUP_KIND;
  3820.         }
  3821.     }
  3822.     else
  3823.         CycleType = POPUP_KIND;
  3824.  
  3825. #ifdef BUILTIN_ZMODEM
  3826.     if(GetVar("terminternalzmodem",PathBuffer,sizeof(PathBuffer),NULL) >= 0)
  3827.     {
  3828.         LONG Value;
  3829.  
  3830.         UseInternalZModem = TRUE;
  3831.  
  3832.         if(StrToLong(PathBuffer,&Value) > 0)
  3833.         {
  3834.             if(!Value)
  3835.                 UseInternalZModem = FALSE;
  3836.         }
  3837.     }
  3838.     else
  3839.         UseInternalZModem = FALSE;
  3840. #endif
  3841.  
  3842.         /* Open OwnDevUnit.library, and allocate a signal bit if it opens. */
  3843.  
  3844.     if(OwnDevUnitBase = OpenLibrary(ODU_NAME,0))
  3845.     {
  3846.             /* If we can't get the signal, ignore the library */
  3847.  
  3848.         if((OwnDevBit = AllocSignal(-1)) == -1)
  3849.         {
  3850.             CloseLibrary(OwnDevUnitBase);
  3851.             OwnDevUnitBase = NULL;
  3852.         }
  3853.     }
  3854.  
  3855.         /* Open workbench.library, don't complain if it fails. */
  3856.  
  3857.     WorkbenchBase = OpenLibrary("workbench.library",0);
  3858.  
  3859.         /* Open icon.library as well, don't complain if it fails either. */
  3860.  
  3861.     IconBase = OpenLibrary("icon.library",0);
  3862.  
  3863.         /* Try to open datatypes.library, just for the fun of it. */
  3864.  
  3865.     DataTypesBase = OpenLibrary("datatypes.library",39);
  3866.  
  3867.     if(!(ConsoleRequest = (struct IOStdReq *)AllocVecPooled(sizeof(struct IOStdReq),MEMF_ANY|MEMF_CLEAR)))
  3868.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CONSOLE_REQUEST_TXT));
  3869.  
  3870.     ConsoleRequest->io_Message.mn_Length = sizeof(struct IOStdReq);
  3871.  
  3872.     if(OpenDevice("console.device",CONU_LIBRARY,(struct IORequest *)ConsoleRequest,NULL))
  3873.         return(LocaleString(MSG_TERMINIT_FAILED_TO_OPEN_CONSOLE_DEVICE_TXT));
  3874.  
  3875.     ConsoleDevice = ConsoleRequest->io_Device;
  3876.  
  3877.     if(!(FakeInputEvent = (struct InputEvent *)AllocVecPooled(sizeof(struct InputEvent),MEMF_ANY|MEMF_CLEAR)))
  3878.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_INPUTEVENT_TXT));
  3879.  
  3880.     FakeInputEvent->ie_Class = IECLASS_RAWKEY;
  3881.  
  3882.     if(!(MacroKeys = (struct MacroKeys *)AllocVecPooled(sizeof(struct MacroKeys),MEMF_ANY|MEMF_CLEAR)))
  3883.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACROKEYS_TXT));
  3884.  
  3885.     if(!(CursorKeys = (struct CursorKeys *)AllocVecPooled(sizeof(struct CursorKeys),MEMF_ANY|MEMF_CLEAR)))
  3886.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_CURSORKEYS_TXT));
  3887.  
  3888.         /* Create the job queue. */
  3889.  
  3890.     if(!(MainJobQueue = CreateJobQueue()))
  3891.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3892.  
  3893.         /* Create the job nodes. */
  3894.  
  3895.     for(i = 0 ; JobInitTable[i].Name ; i++)
  3896.     {
  3897.         if(!(*JobInitTable[i].Node = CreateJob(JobInitTable[i].Name,JobInitTable[i].Type,JobInitTable[i].Function,NULL)))
  3898.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3899.     }
  3900.  
  3901.         /* Create all generic lists. */
  3902.  
  3903.     for(i = GLIST_UPLOAD ; i < GLIST_COUNT ; i++)
  3904.     {
  3905.         if(!(GenericListTable[i] = CreateGenericList()))
  3906.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  3907.     }
  3908.  
  3909.     GenericListTable[GLIST_UPLOAD]->Notify = NotifyUploadQueue;
  3910.  
  3911.         /* Add extra assignments. */
  3912.  
  3913.     AddExtraAssignment("PROGDIR:Fonts","Fonts");
  3914.     AddExtraAssignment("Fonts","Fonts");
  3915.     AddExtraAssignment("PROGDIR:Libs","Libs");
  3916.     AddExtraAssignment("Libs","Libs");
  3917.  
  3918.         /* Set up the attention buffers. */
  3919.  
  3920.     if(!(AttentionBuffers[0] = (STRPTR)AllocVecPooled(SCAN_COUNT * ATTENTION_BUFFER_SIZE,MEMF_ANY|MEMF_CLEAR)))
  3921.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_SEQUENCE_ATTENTION_INFO_TXT));
  3922.  
  3923.     for(i = 1 ; i < SCAN_COUNT ; i++)
  3924.         AttentionBuffers[i] = &AttentionBuffers[i - 1][ATTENTION_BUFFER_SIZE];
  3925.  
  3926.         /* Obtain the default environment storage
  3927.          * path.
  3928.          */
  3929.  
  3930.     if(!ConfigPath)
  3931.     {
  3932.         ConfigPath = PathBuffer;
  3933.  
  3934.         if(!GetEnvDOS("TERMCONFIGPATH",PathBuffer,sizeof(PathBuffer)))
  3935.         {
  3936.             if(!GetEnvDOS("TERMPATH",PathBuffer,sizeof(PathBuffer)))
  3937.             {
  3938.                 APTR LastPtr;
  3939.                 BPTR FileLock;
  3940.  
  3941.                 strcpy(PathBuffer,"TERM:config");
  3942.  
  3943.                 DisableDOSRequesters(&LastPtr);
  3944.  
  3945.                 if(FileLock = Lock("TERM:",ACCESS_READ))
  3946.                     UnLock(FileLock);
  3947.                 else
  3948.                 {
  3949.                     FileLock = DupLock(ThisProcess->pr_HomeDir);
  3950.  
  3951.                         /* Create TERM: assignment referring to
  3952.                          * the directory `term' was loaded from.
  3953.                          */
  3954.  
  3955.                     if(!AssignLock("TERM",FileLock))
  3956.                         UnLock(FileLock);
  3957.                 }
  3958.  
  3959.                 if(!(FileLock = Lock(PathBuffer,ACCESS_READ)))
  3960.                     FileLock = CreateDir(PathBuffer);
  3961.  
  3962.                 if(FileLock)
  3963.                     UnLock(FileLock);
  3964.  
  3965.                 EnableDOSRequesters(LastPtr);
  3966.             }
  3967.         }
  3968.     }
  3969.     else
  3970.     {
  3971.         if(GetFileSize(ConfigPath))
  3972.         {
  3973.             STRPTR Index;
  3974.  
  3975.             strcpy(PathBuffer,ConfigPath);
  3976.  
  3977.             Index = PathPart(PathBuffer);
  3978.  
  3979.             *Index = 0;
  3980.  
  3981.             ConfigFileName = ConfigPath;
  3982.  
  3983.             ConfigPath = PathBuffer;
  3984.         }
  3985.     }
  3986.  
  3987.         /* Check for proper assignment path if necessary. */
  3988.  
  3989.     if(!Strnicmp(ConfigPath,"TERM:",5))
  3990.     {
  3991.         APTR OldPtr;
  3992.         BPTR DirLock;
  3993.  
  3994.             /* Block dos requesters. */
  3995.  
  3996.         DisableDOSRequesters(&OldPtr);
  3997.  
  3998.             /* Try to get a lock on `TERM:' assignment. */
  3999.  
  4000.         if(DirLock = Lock("TERM:",ACCESS_READ))
  4001.             UnLock(DirLock);
  4002.         else
  4003.         {
  4004.                 /* Clone current directory lock. */
  4005.  
  4006.             DirLock = DupLock(ThisProcess->pr_CurrentDir);
  4007.  
  4008.                 /* Create TERM: assignment referring to
  4009.                  * current directory.
  4010.                  */
  4011.  
  4012.             if(!AssignLock("TERM",DirLock))
  4013.                 UnLock(DirLock);
  4014.         }
  4015.  
  4016.         EnableDOSRequesters(OldPtr);
  4017.     }
  4018.  
  4019.         /* Create proper path names. */
  4020.  
  4021.     if(ConfigFileName)
  4022.     {
  4023.         if(!GetFileSize(ConfigFileName))
  4024.             ConfigFileName = NULL;
  4025.     }
  4026.  
  4027.     if(!ConfigFileName)
  4028.     {
  4029.         strcpy(LastConfig,ConfigPath);
  4030.  
  4031.         AddPart(LastConfig,"term.prefs",MAX_FILENAME_LENGTH);
  4032.     }
  4033.     else
  4034.         strcpy(LastConfig,ConfigFileName);
  4035.  
  4036.     strcpy(DefaultPubScreenName,"Workbench");
  4037.  
  4038.         /* Special measure */
  4039.  
  4040.     if(!LastPhone[0])
  4041.         ZapPhonebook = TRUE;
  4042.     else
  4043.         ZapPhonebook = FALSE;
  4044.  
  4045.         /* Create both configuration buffers. */
  4046.  
  4047.     if(!(Config = CreateConfiguration(TRUE)))
  4048.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_PRIMARY_CONFIG_TXT));
  4049.  
  4050.     if(!(PrivateConfig = CreateConfiguration(TRUE)))
  4051.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_SECONDARY_CONFIG_TXT));
  4052.  
  4053.     ResetConfig(Config,ConfigPath);
  4054.  
  4055.     if(ZapPhonebook)
  4056.         LastPhone[0] = 0;
  4057.  
  4058.         /* Read some more environment variables. */
  4059.  
  4060.     GetEnvDOS("EDITOR",Config->PathConfig->Editor,sizeof(Config->PathConfig->Editor));
  4061.  
  4062.         /* Look for the default configuration file. */
  4063.  
  4064.     if(!ReadConfig(LastConfig,Config))
  4065.     {
  4066.         ResetConfig(Config,ConfigPath);
  4067.  
  4068.         Initializing = TRUE;
  4069.  
  4070.         LoadColours = TRUE;
  4071.  
  4072.             /* Now we can safely assume that this is the */
  4073.             /* first invocation of this program on the */
  4074.             /* current setup */
  4075.  
  4076.         FirstInvocation = TRUE;
  4077.     }
  4078.     else
  4079.     {
  4080.         Current2DefaultPalette(Config);
  4081.  
  4082.         if(Config->ScreenConfig->ColourMode == COLOUR_AMIGA)
  4083.             Initializing = FALSE;
  4084.         else
  4085.             Initializing = TRUE;
  4086.     }
  4087.  
  4088.     if(UseNewDevice)
  4089.         strcpy(Config->SerialConfig->SerialDevice,NewDevice);
  4090.  
  4091.     if(UseNewUnit)
  4092.         Config->SerialConfig->UnitNumber = NewUnit;
  4093.  
  4094.     if(Config->MiscConfig->OpenFastMacroPanel)
  4095.         HadFastMacros = TRUE;
  4096.  
  4097.         /* Set up the phonebook file name */
  4098.  
  4099.     if(!LastPhone[0])
  4100.     {
  4101.         if(Config->PhonebookFileName[0])
  4102.             strcpy(LastPhone,Config->PhonebookFileName);
  4103.         else
  4104.         {
  4105.             strcpy(LastPhone,    Config->PathConfig->DefaultStorage);
  4106.             AddPart(LastPhone,    "phonebook.prefs",MAX_FILENAME_LENGTH);
  4107.         }
  4108.     }
  4109.  
  4110.         /* Load the keyboard macros. */
  4111.  
  4112.     strcpy(LastMacros,    Config->PathConfig->DefaultStorage);
  4113.     AddPart(LastMacros,    "functionkeys.prefs",MAX_FILENAME_LENGTH);
  4114.  
  4115.     if(Config->MacroFileName[0])
  4116.     {
  4117.         if(LoadMacros(Config->MacroFileName,MacroKeys))
  4118.             strcpy(LastMacros,Config->MacroFileName);
  4119.         else
  4120.         {
  4121.             if(!LoadMacros(LastMacros,MacroKeys))
  4122.                 ResetMacroKeys(MacroKeys);
  4123.         }
  4124.     }
  4125.     else
  4126.     {
  4127.         if(!LoadMacros(LastMacros,MacroKeys))
  4128.             ResetMacroKeys(MacroKeys);
  4129.     }
  4130.  
  4131.         /* Load the cursor keys. */
  4132.  
  4133.     strcpy(LastCursorKeys,    Config->PathConfig->DefaultStorage);
  4134.     AddPart(LastCursorKeys,    "cursorkeys.prefs",MAX_FILENAME_LENGTH);
  4135.  
  4136.     if(Config->CursorFileName[0])
  4137.     {
  4138.         if(ReadIFFData(Config->CursorFileName,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  4139.             strcpy(LastCursorKeys,Config->CursorFileName);
  4140.         else
  4141.         {
  4142.             if(!ReadIFFData(LastCursorKeys,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  4143.                 ResetCursorKeys(CursorKeys);
  4144.         }
  4145.     }
  4146.     else
  4147.     {
  4148.         if(!ReadIFFData(LastCursorKeys,CursorKeys,sizeof(struct CursorKeys),ID_KEYS))
  4149.             ResetCursorKeys(CursorKeys);
  4150.     }
  4151.  
  4152.         /* Load the sound settings. */
  4153.  
  4154.     strcpy(LastSound,    Config->PathConfig->DefaultStorage);
  4155.     AddPart(LastSound,    "sound.prefs",MAX_FILENAME_LENGTH);
  4156.  
  4157.     if(Config->SoundFileName[0])
  4158.     {
  4159.         if(ReadIFFData(Config->SoundFileName,&SoundConfig,sizeof(struct SoundConfig),ID_SOUN))
  4160.             strcpy(LastSound,Config->SoundFileName);
  4161.         else
  4162.         {
  4163.             if(!ReadIFFData(LastSound,&SoundConfig,sizeof(struct SoundConfig),ID_SOUN))
  4164.                 ResetSound(&SoundConfig);
  4165.         }
  4166.     }
  4167.     else
  4168.     {
  4169.         if(!ReadIFFData(LastSound,&SoundConfig,sizeof(struct SoundConfig),ID_SOUN))
  4170.             ResetSound(&SoundConfig);
  4171.     }
  4172.  
  4173.         /* Initialize the sound support routines. */
  4174.  
  4175.     SoundInit();
  4176.  
  4177.         /* Load the phone number pattern / rates settings. */
  4178.  
  4179.     strcpy(LastPattern,    Config->PathConfig->DefaultStorage);
  4180.     AddPart(LastPattern,    "rates.prefs",MAX_FILENAME_LENGTH);
  4181.  
  4182.     if(Config->AreaCodeFileName[0])
  4183.     {
  4184.         if(PatternList = LoadTimeDateList(Config->AreaCodeFileName,&ErrorCode))
  4185.             strcpy(LastPattern,Config->AreaCodeFileName);
  4186.         else
  4187.             PatternList = LoadTimeDateList(LastPattern,&ErrorCode);
  4188.     }
  4189.     else
  4190.         PatternList = LoadTimeDateList(LastPattern,&ErrorCode);
  4191.  
  4192.     if(!(GlobalPhoneHandle = CreatePhonebook(0,FALSE)))
  4193.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4194.  
  4195.     if(!PatternList)
  4196.     {
  4197.         if(!(PatternList = CreateList()))
  4198.             return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4199.     }
  4200.  
  4201.         /* Load the translation tables. */
  4202.  
  4203.     strcpy(LastTranslation,        Config->PathConfig->DefaultStorage);
  4204.     AddPart(LastTranslation,    "translation.prefs",MAX_FILENAME_LENGTH);
  4205.  
  4206.     if(Config->TranslationFileName[0])
  4207.     {
  4208.         if(LoadDefaultTranslationTables(Config->TranslationFileName))
  4209.             strcpy(LastTranslation,Config->TranslationFileName);
  4210.         else
  4211.             LoadDefaultTranslationTables(LastTranslation);
  4212.     }
  4213.     else
  4214.         LoadDefaultTranslationTables(LastTranslation);
  4215.  
  4216.         /* Load the fast! macro settings. */
  4217.  
  4218.     strcpy(LastFastMacros,        Config->PathConfig->DefaultStorage);
  4219.     AddPart(LastFastMacros,        "fastmacros.prefs",MAX_FILENAME_LENGTH);
  4220.  
  4221.     if(Config->FastMacroFileName[0])
  4222.     {
  4223.         if(LoadFastMacros(Config->FastMacroFileName,&FastMacroList))
  4224.             strcpy(LastFastMacros,Config->FastMacroFileName);
  4225.         else
  4226.             LoadFastMacros(LastFastMacros,&FastMacroList);
  4227.     }
  4228.     else
  4229.         LoadFastMacros(LastFastMacros,&FastMacroList);
  4230.  
  4231.     FastMacroCount = GetListSize(&FastMacroList);
  4232.  
  4233.         /* Load the speech settings. */
  4234.  
  4235.     strcpy(LastSpeech,    Config->PathConfig->DefaultStorage);
  4236.     AddPart(LastSpeech,    "speech.prefs",MAX_FILENAME_LENGTH);
  4237.  
  4238.     if(Config->SpeechFileName[0])
  4239.     {
  4240.         if(ReadIFFData(Config->SpeechFileName,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  4241.             strcpy(LastSpeech,Config->SpeechFileName);
  4242.         else
  4243.         {
  4244.             if(!ReadIFFData(LastSpeech,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  4245.                 ResetSpeechConfig(&SpeechConfig);
  4246.         }
  4247.     }
  4248.     else
  4249.     {
  4250.         if(!ReadIFFData(LastSpeech,&SpeechConfig,sizeof(struct SpeechConfig),ID_SPEK))
  4251.             ResetSpeechConfig(&SpeechConfig);
  4252.     }
  4253.  
  4254.         /* Load the hotkey settings. */
  4255.  
  4256.     strcpy(LastKeys,    Config->PathConfig->DefaultStorage);
  4257.     AddPart(LastKeys,    "hotkeys.prefs",MAX_FILENAME_LENGTH);
  4258.  
  4259.     if(Config->HotkeyFileName[0])
  4260.     {
  4261.         if(LoadHotkeys(Config->HotkeyFileName,&Hotkeys))
  4262.             strcpy(LastKeys,Config->HotkeyFileName);
  4263.         else
  4264.         {
  4265.             if(!LoadHotkeys(LastKeys,&Hotkeys))
  4266.                 ResetHotkeys(&Hotkeys);
  4267.         }
  4268.     }
  4269.     else
  4270.     {
  4271.         if(!LoadHotkeys(LastKeys,&Hotkeys))
  4272.             ResetHotkeys(&Hotkeys);
  4273.     }
  4274.  
  4275.         /* Load the trap settings. */
  4276.  
  4277.     strcpy(LastTraps,    Config->PathConfig->DefaultStorage);
  4278.     AddPart(LastTraps,    "trap.prefs",MAX_FILENAME_LENGTH);
  4279.  
  4280.     WatchTraps = TRUE;
  4281.  
  4282.     if(Config->TrapFileName[0])
  4283.     {
  4284.         if(LoadTraps(Config->TrapFileName,GenericListTable[GLIST_TRAP]))
  4285.             strcpy(LastTraps,Config->TrapFileName);
  4286.         else
  4287.         {
  4288.             if(!LoadTraps(LastTraps,GenericListTable[GLIST_TRAP]))
  4289.                 WatchTraps = FALSE;
  4290.         }
  4291.     }
  4292.     else
  4293.     {
  4294.         if(!LoadTraps(LastTraps,GenericListTable[GLIST_TRAP]))
  4295.             WatchTraps = FALSE;
  4296.     }
  4297.  
  4298.         /* Set up the text pacing controls. */
  4299.  
  4300.     SendSetup();
  4301.  
  4302.         /* Set up the capture parser. */
  4303.  
  4304.     if(!(ParserStuff = CreateParseContext()))
  4305.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4306.  
  4307.     if(!CaptureParserInit())
  4308.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4309.  
  4310.         /* Are we to freeze the text buffer? */
  4311.  
  4312.     if(!Config->CaptureConfig->BufferEnabled)
  4313.         BufferFrozen = TRUE;
  4314.  
  4315.     ConOutputUpdate();
  4316.  
  4317.         /* Initialize the data flow parser. */
  4318.  
  4319.     ResetDataFlowFilter();
  4320.  
  4321.         /* Set up parsing jump tables. */
  4322.  
  4323.     if(!(SpecialTable = (JUMP *)AllocVecPooled(256 * sizeof(JUMP),MEMF_CLEAR | MEMF_ANY)))
  4324.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4325.  
  4326.     for(i = 0 ; i < sizeof(SpecialKeys) / sizeof(struct SpecialKey) ; i++)
  4327.         SpecialTable[SpecialKeys[i].Key] = (JUMP)SpecialKeys[i].Routine;
  4328.  
  4329.     if(!(AbortTable = (JUMP *)AllocVecPooled(256 * sizeof(JUMP),MEMF_ANY)))
  4330.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4331.  
  4332.     for(i = 0 ; i < 256 ; i++)
  4333.     {
  4334.         switch(AbortMap[i])
  4335.         {
  4336.             case 0:
  4337.  
  4338.                 AbortTable[i] = (JUMP)ParseCode;
  4339.                 break;
  4340.  
  4341.             case 1:
  4342.  
  4343.                 AbortTable[i] = (JUMP)DoCancel;
  4344.                 break;
  4345.  
  4346.             case 2:
  4347.  
  4348.                 AbortTable[i] = (JUMP)DoNewEsc;
  4349.                 break;
  4350.  
  4351.             case 3:
  4352.  
  4353.                 AbortTable[i] = (JUMP)DoNewCsi;
  4354.                 break;
  4355.         }
  4356.     }
  4357.  
  4358.     if(!(TrapStuff = CreateParseContext()))
  4359.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4360.  
  4361.         /* Create the special event queue. */
  4362.  
  4363.     if(!(SpecialQueue = CreateMsgQueue(NULL,0)))
  4364.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4365.  
  4366.         /* Set up the serial driver. */
  4367.  
  4368.     if(!CreateSerial(LocalBuffer,sizeof(LocalBuffer)))
  4369.     {
  4370.         ShowRequest(NULL,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),LocalBuffer);
  4371.  
  4372.         DeleteSerial();
  4373.     }
  4374.  
  4375.         /* Get a signal bit. */
  4376.  
  4377.     if((CheckBit = AllocSignal(-1)) == -1)
  4378.         return(LocaleString(MSG_TERMINIT_FAILED_TO_GET_CHECK_SIGNAL_TXT));
  4379.  
  4380.         /* Add the global term port. */
  4381.  
  4382.     Forbid();
  4383.  
  4384.     if(!(TermPort = (struct TermPort *)FindPort(TERMPORTNAME)))
  4385.     {
  4386.         if(!(TermPort = (struct TermPort *)AllocVec(sizeof(struct TermPort) + sizeof(TERMPORTNAME),MEMF_PUBLIC|MEMF_CLEAR)))
  4387.             return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_GLOBAL_PORT_TXT));
  4388.         else
  4389.         {
  4390.             NewList(&TermPort->MsgPort.mp_MsgList);
  4391.             InitSemaphore(&TermPort->OpenSemaphore);
  4392.  
  4393.             TermPort->MsgPort.mp_Flags            = PA_IGNORE;
  4394.             TermPort->MsgPort.mp_Node.ln_Name    = (char *)(TermPort + 1);
  4395.  
  4396.             strcpy(TermPort->MsgPort.mp_Node.ln_Name,TERMPORTNAME);
  4397.  
  4398.             AddPort(&TermPort->MsgPort);
  4399.         }
  4400.     }
  4401.  
  4402.     Permit();
  4403.  
  4404.         /* Keep another term task from removing the port. */
  4405.  
  4406.     TermPort->HoldIt = TRUE;
  4407.  
  4408.         /* Install a new term process. */
  4409.  
  4410.     ObtainSemaphore(&TermPort->OpenSemaphore);
  4411.  
  4412.     TermPort->OpenCount++;
  4413.  
  4414.     TermPort->HoldIt = FALSE;
  4415.  
  4416.     TermID = TermPort->ID++;
  4417.  
  4418.     ReleaseSemaphore(&TermPort->OpenSemaphore);
  4419.  
  4420.         /* Set up the ID string. */
  4421.  
  4422.     if(TermID)
  4423.         LimitedSPrintf(sizeof(TermIDString),TermIDString,"TERM.%ld",TermID);
  4424.     else
  4425.         strcpy(TermIDString,"TERM");
  4426.  
  4427.     if(RexxPortName[0])
  4428.     {
  4429.         for(i = 0 ; i < strlen(RexxPortName) ; i++)
  4430.             RexxPortName[i] = ToUpper(RexxPortName[i]);
  4431.  
  4432.         if(FindPort(RexxPortName))
  4433.             RexxPortName[0] = 0;
  4434.     }
  4435.  
  4436.     if(!RexxPortName[0])
  4437.         strcpy(RexxPortName,TermIDString);
  4438.  
  4439.         /* Install the hotkey handler. */
  4440.  
  4441.     SetupCx();
  4442.  
  4443.         /* Allocate the first few lines for the display buffer. */
  4444.  
  4445.     if(!CreateBuffer())
  4446.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_VIEW_BUFFER_TXT));
  4447.  
  4448.     if(!(XprIO = (struct XPR_IO *)AllocVec(sizeof(struct XPR_IO),MEMF_ANY|MEMF_CLEAR)))
  4449.         return(LocaleString(MSG_TERMINIT_FAILED_TO_CREATE_PROTOCOL_BUFFER_TXT));
  4450.  
  4451.         /* Set up the external emulation macro data. */
  4452.  
  4453.     if(!(XEM_MacroKeys = (struct XEmulatorMacroKey *)AllocVecPooled((2 + 10 * 4) * sizeof(struct XEmulatorMacroKey),MEMF_ANY|MEMF_CLEAR)))
  4454.         return(LocaleString(MSG_TERMINIT_FAILED_TO_ALLOCATE_MACRO_KEY_DATA_TXT));
  4455.  
  4456.         /* Set up the default transfer protocol */
  4457.  
  4458.     if(Config->TransferConfig->DefaultType == XFER_XPR)
  4459.         strcpy(LastXprLibrary,Config->TransferConfig->DefaultLibrary);
  4460.  
  4461.     ProtocolSetup(FALSE);
  4462.  
  4463.         /* Load a keymap file if required. */
  4464.  
  4465.     if(Config->TerminalConfig->KeyMapFileName[0])
  4466.         KeyMap = LoadKeyMap(Config->TerminalConfig->KeyMapFileName);
  4467.  
  4468.     if(!(TermRexxPort = (struct MsgPort *)CreateMsgPort()))
  4469.         return(LocaleString(MSG_GLOBAL_FAILED_TO_CREATE_MSGPORT_TXT));
  4470.  
  4471.         /* If rexxsyslib.library opens cleanly it's time for
  4472.          * us to create the background term Rexx server.
  4473.          */
  4474.  
  4475.     if(RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME,0))
  4476.     {
  4477.             /* Create a background process handling the
  4478.              * rexx messages asynchronously.
  4479.              */
  4480.  
  4481.         if(!(RexxProcess = StartProcessWaitForHandshake("term Rexx Process",(TASKENTRY)RexxServer,
  4482.             NP_Priority,    5,
  4483.             NP_StackSize,    8000,
  4484.             NP_WindowPtr,    -1,
  4485.         TAG_END)))
  4486.             return(LocaleString(MSG_TERMINIT_UNABLE_TO_CREATE_AREXX_PROCESS_TXT));
  4487.     }
  4488.  
  4489.         /* Install the public screen name, assumes that the user
  4490.          * wants the window to be opened on the screen, rather than
  4491.          * opening a custom screen.
  4492.          */
  4493.  
  4494.     if(SomePubScreenName[0])
  4495.     {
  4496.         strcpy(Config->ScreenConfig->PubScreenName,SomePubScreenName);
  4497.  
  4498.         Config->ScreenConfig->Blinking        = FALSE;
  4499.         Config->ScreenConfig->FasterLayout    = FALSE;
  4500.         Config->ScreenConfig->UseWorkbench    = TRUE;
  4501.  
  4502.         SomePubScreenName[0] = 0;
  4503.     }
  4504.  
  4505.     CreateQueueProcess();
  4506.  
  4507.     AddRendezvousSemaphore(RexxPortName);
  4508.  
  4509.         /* Start the rates accounting */
  4510.  
  4511.     if(!CreateAccountant())
  4512.         return(LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT));
  4513.  
  4514.     if(DoIconify)
  4515.         return(NULL);
  4516.     else
  4517.     {
  4518.             /* Create the whole display. */
  4519.  
  4520.         if(Result = CreateDisplay(FALSE,!KeepQuiet))
  4521.             return(Result);
  4522.         else
  4523.         {
  4524.             PubScreenStuff();
  4525.  
  4526.             UpdateJob(MainJobQueue,SerialCheckJob,SIG_CHECK);
  4527.             UpdateJob(MainJobQueue,QueueJob,SIG_QUEUE);
  4528.  
  4529.             if(OwnDevUnitBase)
  4530.                 UpdateJob(MainJobQueue,OwnDevUnitJob,SIG_OWNDEVUNIT);
  4531.  
  4532.             if(TermRexxPort)
  4533.                 UpdateJob(MainJobQueue,RexxJob,SIG_REXX);
  4534.  
  4535.             return(NULL);
  4536.         }
  4537.     }
  4538. }
  4539.